1
votes

I have some named routes in a controller named VehicleController:

vehicle.index
vehicle.show

And then I have an admin section, where I have defined a route group with prefix and middleware. In this section I have a resource controller name AdminVehicleController to handle CRUD tasks for the Vehicle (not sure if this is best practice) with the following routes:

vehicle.index
vehicle.create
vehicle.store
...

However these named routes are conflicting. My routes web.php looks like this for now:

Route::get('vehicles', 'VehicleController@index')->name('vehicle.index');
Route::get('vehicle/{vehicle}', 'VehicleController@show')->name('vehicle.show');

Route::group(['prefix' => 'admin', 'middleware' => 'is.admin'], function () {
    Route::get('/', 'AdminDashboardController@index');
    Route::resource('vehicle', 'AdminVehicleController');
});

If I add 'name' => 'admin' to the Route::group() array, the route names will be adminvehicle.index and not admin.vehicle.index.

What is the correct way to combine all these parameters in the route?

2
you can use as key, 'as'=> 'admin.', notice the . at end of admin. refjagad89
I tried that, but for the admin (dashboard URL) the named route will be admin. - Not a huge problem, but annoying.rebellion
you can also name it as dashboard so it will be admin.dashboard, so that will be much cleaner.jagad89

2 Answers

4
votes

Try to use as parameter for your admin group

Route::group(['prefix' => 'admin', 'middleware' => 'is.admin', 'as'=> 'admin.'], function () {
    Route::get('/', 'AdminDashboardController@index')->name('dashboard');
    Route::resource('vehicle', 'AdminVehicleController');
});

Reference Link

1
votes

Supply a names array as part of the third parameter $options array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.

    Route::resource('vehicle', 'AdminVehicleController', [
        'names' => [
            'index' => 'admin.vehicle.index',
            // etc...
        ]
    ]);