user1785900
user1785900

Reputation:

two different controllers with same route name

Assume I have two controllers named Client\DashboardController and Admin\DashboardController and routes.php contains the following:

Route::get('admin/', [
    'uses' => 'Admin\DashboardController',
    'as'   => 'dashboard'
]);

Route::get('client/', [
    'uses' => 'Client\DashboardController',
    'as'   => 'dashboard'
]);

both controllers are in different namespace but share one view. In my view I would like to do the following:

<a href="{{ route('dashboard') }}">home</a>

and depending on the url prefix it should maps to the correct controller, but the issue is it always maps to Client controller.

Upvotes: 1

Views: 953

Answers (1)

Ben
Ben

Reputation: 16553

That's like wanting to have two function with the same name just for the sake of it and complain about the compiler for not doing what you want to.

Just namespace your routes, it'll make you life (debugging and maintaining) easier:

Route::get('admin/', [
    'uses' => 'Admin\DashboardController',
    'as'   => 'admin.dashboard'
]);

Route::get('client/', [
    'uses' => 'Client\DashboardController',
    'as'   => 'client.dashboard'
]);

And then:

<a href="{{ route('admin.dashboard') }}">home</a>
<a href="{{ route('client.dashboard') }}">home</a>

Upvotes: 1

Related Questions