Auto generate routes for Laravel Livewire Components.
- Livewire 2
- Laravel 8
- php 8
I recommend that you try Spatie Laravel Route Attributes before installing this package.
composer require tanthammar/livewire-auto-routes
You can use web.php as normal. Routes declared in your Livewire components are registered after the routes in web.php.
- You generate routes via traits or by adding a
route()method to your Livewire component. - Your Livewire components can exist in any folder inside the
appnamespace. - If you don't add any of the traits or the
route()method, the Livewire component is treated just as a normal component, thus ignored by this package.
- Applies the
guestmiddleware. - The property is used to generate both the route name and url.
use Tanthammar\LivewireAutoRoutes\HasGuestRoute;
class FooComponent extends \Livewire\Component
{
use HasGuestRoute;
protected string $guestRoute = '/foo/{id}/edit'; //route name becomes 'foo.id.edit'
}- Applies the
authmiddleware. - The property is used to generate both the route name and url.
use Tanthammar\LivewireAutoRoutes\HasAuthRoute;
class FooComponent extends \Livewire\Component
{
use HasAuthRoute;
protected string $authRoute = '/foo/{name?}'; //route name becomes 'foo.name'
}Declare the route just like you would in web.php
use Illuminate\Support\Facades\Route;
class FooComponent extends \Livewire\Component
{
public function route(): \Illuminate\Routing\Route|array
{
return Route::get('foo', static::class)
->middleware('auth') //default middleware is 'web'
->name('foo');
}
}The RouteMaker can auto-generate the route name from the route definition, but it's optional.
use Tanthammar\LivewireAutoRoutes\RouteMaker;
class FooComponent extends \Livewire\Component
{
public function route(): RouteMaker
{
return new RouteMaker(
route: 'users/{id}/edit', //if you omit $name, this route name will become 'users.id.edit'
middleware: ['auth:sanctum', 'verified'],
component: static::class,
name: 'users.edit' //OPTIONAL, else $name will be generated from $route
);
}
}Livewire component FILES are looped in alphabetical order in the app namespace.
One way to control the load order is to group your components in subfolders with suitable names
like routeGroupA, routeGroupB, where routes in "routeGroupA" would be registered before "routeGroupB".
It's recommended to keep a controlled naming structure to avoid route conflicts. Use the RouteMaker if you want better naming.
| Directory(asc) = load order | $authRoute or $guestRoute | Generated route name |
|---|---|---|
| App/Foo/Users/Create.php | users/create | users.create |
| App/Foo/Users/CustomStuff.php | users/custom-stuff/{id} | users.custom-stuff.id |
| App/Foo/Users/Delete.php | users/delete/{id} | users.delete.id |
| App/Foo/Users/Edit.php | users/edit/{id} | users.edit.id |
| App/Foo/Users/Index.php | users | users |
| App/Foo/Users/Show.php | users/{id} | users.id |
Discuss with other tall-form users on the official Livewire Discord channel. You'll find me in the "partners/tall-forms" channel.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security-related issues, please open an issue.
The MIT License (MIT). Please see License File for more information.