Install the Package
composer require spiral-packages/livewire
Ensure your composer.json meets the requirements (PHP 8.1+, Spiral 3.7+).
Register the Middleware
Add the Livewire middleware to your Spiral application’s HTTP pipeline in config/app.php:
'http' => [
'middleware' => [
// ...
\Spiral\Livewire\Middleware\HandleLivewireRequests::class,
],
],
First Livewire Component
Create a component class (e.g., app/Components/HelloWorld.php):
namespace App\Components;
use Spiral\Livewire\Component;
class HelloWorld extends Component
{
public string $name = 'World';
public function render()
{
return view('livewire.hello-world');
}
}
Register it in config/livewire.php:
'components' => [
\App\Components\HelloWorld::class,
],
Blade View
Create resources/views/livewire/hello-world.blade.php:
<div>
<h1>Hello, {{ $name }}!</h1>
<input wire:model="name" type="text">
</div>
Route and Use
Add a route in config/routes.php:
$router->get('/hello', \Spiral\Livewire\Livewire::class)
->bind('hello');
Access /hello in your browser.
wire:model="name").wire:click or wire:change for dynamic interactions without full page reloads.Initialization
Override boot() for one-time setup (e.g., fetching initial data):
public function boot()
{
$this->data = Model::query()->first();
}
Rules and Validation
Use Laravel’s validation rules in rules():
public function rules()
{
return [
'name' => 'required|min:3',
];
}
Event Handling
Emit events with emit() for client-side JavaScript:
public function save()
{
$this->validate();
$this->emit('message', 'Saved!');
}
Dependency Injection Inject Spiral services into components:
use Spiral\Core\Container;
public function __construct(
private Container $container
) {}
public function mount()
{
$this->data = $this->container->get(MyService::class);
}
Middleware and Auth Protect components with Spiral middleware (e.g., auth):
$router->get('/dashboard', \Spiral\Livewire\Livewire::class)
->middleware(\Spiral\Http\Middleware\AuthMiddleware::class);
Asset Management Use Spiral’s asset pipeline for Livewire scripts/styles:
// config/livewire.php
'assets' => [
'scripts' => [
'livewire.js',
],
],
CRUD Operations
mount().wire:model and save with a method.Modals and Dialogs
Use wire:click to toggle visibility and pass data via component properties.
Pagination
Implement mount() with query parameters and use wire:model.debounce for search.
Caching Issues
php spiral cache:clear) after adding new components.php artisan livewire:discover if needed.Middleware Conflicts
HandleLivewireRequests is last in the pipeline to avoid premature request termination.PHP 8.1+ Features
wire:model (e.g., wire:model.live="name").wire:init; use mount() instead.Logs
Enable Livewire debug mode in config/livewire.php:
'debug' => env('APP_DEBUG', false),
Check Spiral logs (storage/logs/) for Livewire errors.
Wire:Key Conflicts
Ensure wire:key is unique for dynamic components (e.g., <div wire:key="item-{{ $id }}">).
CSRF Tokens
Spiral’s CSRF middleware must be enabled for Livewire to work. Verify in config/app.php:
'http' => [
'middleware' => [
\Spiral\Http\Middleware\CsrfProtectionMiddleware::class,
],
],
Type Safety Use PHP 8.1+ typed properties and return types:
public function getName(): string
{
return $this->name;
}
Testing Test components with Spiral’s HTTP client:
$response = $this->get('/livewire/hello');
$response->assertLivewireLoaded('hello-world');
Performance
mount().wire:ignore for non-reactive elements (e.g., <div wire:ignore>).Extending Spiral
Create custom Livewire directives by extending Spiral\Livewire\LivewireServiceProvider.
Custom Directives
Register in boot():
Livewire::directive('custom', function ($expression) {
return "alert('{$expression}')";
});
Global JavaScript
Add to resources/js/app.js:
document.addEventListener('livewire:init', () => {
Livewire.on('message', (message) => alert(message));
});
Component Factories Use Spiral’s DI container to resolve components dynamically:
$component = $this->container->get(\App\Components\HelloWorld::class);
How can I help you explore Laravel packages today?