Best way to group your livewire routes?

I used to have a file called livewire.php and registered it in the RouteServiceProvider with a namespace of App\\Http\\Livewire.

So that it shorten my route from:

Route::get('/categories', App\Http\Livewire\Category\ManageCategories::class);

To a simple route like:

Route::get('/categories', Category\ManageCategories::class);

However, we’re going to transfer all our routes to web.php and it’s back to the long route. Is there any way to clean this route with calling App\Http\Livewire everytime?

The only solution I can think of is this:

use App\Http\Livewire\Category\EditCategory;
use App\Http\Livewire\Category\CreateCategory;
use App\Http\Livewire\Category\ManageCategories;

An alternative if you don’t want a bunch of use statements at the top of your web.php and don’t want to use the FQN is to wrap your routes in a group and provide a namespace.

So in your scenario:

Route::group(['prefix' => '/categories', 'namespace' => 'App\Http\Livewire\Category'], function () {
    Route::get('/', ManageCategories::class);
    Route::get('/create', CreateCategory::class);
    Route::get('/categories/{id}/edit', EditCategory::class);
});

Most people expect use statements at the top and often review them to determine what is in the class/file. Most text/code editors have the ability to collapse/fold use statements too, so don’t be too worried if you have a large number of them.

Your choice at the end of the day though.

1 Like

A simple:

use App\Http\Livewire\Category;

should let you use the route you aim for:

Route::get('/categories', Category\ManageCategories::class);