Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

engrlaravel's avatar

different routes for different roles

Is it possible in laravel to use different route path for different roles?

For example:

role= candidate using this path
    Route::get('dashboard', 'DashboardController@index');

role=admin can use this path
    Route::get('dashboard', 'HomeController@index');

in routes/web.php file

0 likes
2 replies
bobbybouwmann's avatar

As far as I know you can't do it like that. The routes can be cached in your application and therefore you can't make decisions based on the current role of the logged in user. However, you can easily do this in one controller

public function index()
{
    if (auth()->user()->role === 'admin') {
        return view('admin.dashboard');
    }

    return view('dashboard');
}

Another solution for this is having a dashboard route per role based on the url.

Route::prefix('admin')->middleware('role:admin')->group(function () {
    Route::get('dashboard', 'DashboardController@index'); // example.com/admin/dashboard
});

Route::get('dashboard', 'HomeController@index'); // example.com/dashboard

This is way cleaner and more readable ;)

jlrdw's avatar

Not trying to over write anything @bobbybouwmann said, but I think it would not hurt you to view Jeffrey's free "from scratch" videos. He has one covering authentication and another introducing authorization.

Please or to participate in this conversation.