如何在 web.php 的 if 语句中调用控制器函数?
我在 web.php 中有一条路由,如下所示:
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 control function in that if statements?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于您知道有更好的方法可以做到这一点,请参阅 https://laravel.com/docs/master/redirects#redirecting-controller-actions" rel="nofollow noreferrer">https:// /laravel.com/docs/master/redirects#redirecting-controller-actions
Given that you are aware that there are better ways of doing this, see https://laravel.com/docs/master/redirects#redirecting-controller-actions
将所有 if-else 语句写入控制器中。
在controller中创建一个方法名index并写入上面的代码
write all the if-else statements in the controller.
In controller create a method name index and write the above code
您可以使用中间件创建只能由特定角色访问的路由组,请参阅此处的文档
https://laravel.com/docs/9.x/middleware
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