nodus-it/livewire-core

An livewire core package

v0.5 2024-03-18 14:04 UTC

This package is auto-updated.

Last update: 2024-11-22 12:14:05 UTC


README

License Latest Unstable Version Total Downloads Build Status codecov

The core package for all Laravel Livewire packages from Nodus-IT.

This package is currently being developed and is still in testing

Installation

You can install the package via composer:

composer require nodus-it/livewire-core

You can publish the config file with:

php artisan vendor:publish --provider="Nodus\Packages\LivewireCore\LivewireCoreServiceProvider" --tag="livewire-core:config"

You can publish the blade views with:

php artisan vendor:publish --provider="Nodus\Packages\LivewireCore\LivewireCoreServiceProvider" --tag="livewire-core:views"

Usage

Controller integration

All our Livewire components can be used as standalone in your blade views as you are used to it. But we are offering an optional way to integrate a Livewire component so to say as full page component into your controllers.

In order to use this, you need to use the SupportsLivewire trait in your controller. This trait provides a single function named livewire() where you can pass your component class and an array with parameters:

public function index()
{
    return $this->livewire(UserListView::class, ['builder' => User::query()]);
}

By default, this would be extending a layout called layouts.app and adding the component to a section called content.

Customize layout or section

In order to customize the used layout and/or section you have two possible ways. First you could use the fluent interface of the LivewireComponent class, which is returned when calling the livewire() method. With that you can just pass your desired settings via the following methods:

public function index()
{
    return $this->livewire(UserListView::class, ['builder' => User::query()])
        ->layout('myFolder.layoutName', ['title' => '...'])
        ->section('myContentSection');
}

In case you don't want to define this on every livewire() call, you can also add some properties and methods to your controller, with which you can define the default layout, the layout parameters and the default section for all livewire() calls of this controller:

protected $defaultLayout = 'myFolder.layoutName';

protected $defaultSection = 'myContentSection';

private function getLayoutData()
{
    return [
        'title' => $this->title,
    ];
}

CSP nonce handling

This package provides the support for the Content Security Policy (CSP) nonce for all assets in the Laravel Livewire packages from Nodus-IT. To use it you need to add your CSP nonce provider callback to the config:

// Possible values: "<nonce_callable>", "null"
'csp_nonce'   => 'my_nonce_provider_function',

The generated nonce is then automatically added to all assets provided by our packages.

For ease of use we added also a Blade Directive for adding the nonce HTML attribute to your custom assets:

<script @nonce>
...
</script>

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.