Mr.Singh
Mr.Singh

Reputation: 1781

Laravel: Multiple routes with same location and name

In my laravel(7.x) application, I am trying to bind two routes admin/ and admin/dashboard with same name. While running the php artisan route:list command, I am getting an error that Unable to prepare route [admin/dasboard] for serialization. Another route has already been assigned name [admin.dashboard].

Web.php

Route::group([ 'prefix' => 'admin' ], function() {
    ...

    /**
     * Dashboard
     */
    Route::get('/', 'Backend\DashboardController@index')->name('admin.dashboard');
    Route::get('/dasboard', 'Backend\DashboardController@index')->name('admin.dashboard');
});

It was working fine in the previous versions of laravel.

How to fix this..?

Upvotes: 3

Views: 17097

Answers (3)

Mr.Singh
Mr.Singh

Reputation: 1781

Thank you @Sehdev...

Here is the final code that I am using. Although, even with both routes mentioned in the web.php, you can only see the route in the browser, that is written at end, which in my case is /dashboard. However, both (/, /dashboard) route are working now.

Route::namespace('Backend')->prefix('admin')->group(function() {
    ...

    /**
     * Dashboard
     */
    Route::get('/', 'DashboardController@index')->name('admin.dashboard');
    Route::get('/dashboard', 'DashboardController@index')->name('admin.dashboard');
});

Many thanks again :)

Upvotes: 1

user379888
user379888

Reputation:

You can't have two routes with the same names.

Route::group([ 'prefix' => 'admin' ], function() {
    ...

    /**
     * Dashboard
     */
    Route::get('/', 'Backend\DashboardController@index')->name('home.dashboard');
    Route::get('/dasboard', 'Backend\DashboardController@index')->name('admin.dashboard');
});

Upvotes: 0

Sehdev
Sehdev

Reputation: 5662

You are using named routes i.e. ->name(admin.dashboard) twice but named route must be unique that is why you are getting error

 Route::get('/', 'Backend\DashboardController@index')->name('admin.dashboard');
 Route::get('/dasboard', 'Backend\DashboardController@index')->name('admin.dashboard');

To solve this change any one of your route to something else for e.g

Route::get('/', 'Backend\DashboardController@index')->name('admin'); // changed admin.dashboard to admin
Route::get('/dasboard', 'Backend\DashboardController@index')->name('admin.dashboard');

Upvotes: 3

Related Questions