Home > Enterprise >  How to call a controller function inside an if statement in web.php?
How to call a controller function inside an if statement in web.php?

Time:03-06

I have a route in web.php that looks like this:

Route::get('/dashboard', function () {
    if (Auth::user()->type === 'admin') {
        return view('adminDashboard');
    } elseif (Auth::user()->type === 'manager') {
        // Here I want to call ManagerController@managerDashboard function
    } elseif (Auth::user()->type === 'user') {
        return view('UserDashboard');
    } else return redirect('404');
})->middleware(['auth'])->name('dashboard');

How can I call a controller function in that if statement?

CodePudding user response:

You can use middleware to make a route group that only can be accessed by specific role, see the docs here

https://laravel.com/docs/9.x/middleware

CodePudding user response:

Given that you are aware that there are better ways of doing this, see https://laravel.com/docs/master/redirects#redirecting-controller-actions

return redirect()->action([ManagerController::class, 'managerDashboard']);
  • Related