如何在 web.php 的 if 语句中调用控制器函数?

发布于 2025-01-12 00:57:14 字数 545 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别闹i 2025-01-19 00:57:14

鉴于您知道有更好的方法可以做到这一点,请参阅 https://laravel.com/docs/master/redirects#redirecting-controller-actions" rel="nofollow noreferrer">https:// /laravel.com/docs/master/redirects#redirecting-controller-actions

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

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']);
与酒说心事 2025-01-19 00:57:14

将所有 if-else 语句写入控制器中。

Route::get('/dashboard', 'DashboardController@index')->middleware(['auth'])->name('dashboard');

在controller中创建一个方法名index并写入上面的代码

    public function __construct(){
        $this->middleware('auth');
    }

    public function index(){
    if (Auth::user()->type === 'admin') {
            return view('adminDashboard');
        } 
elseif (Auth::user()->type === 'manager') {
           return view('ManagerDashboard')
        }
 elseif (Auth::user()->type === 'user') {
            return view('UserDashboard');
        } 
else return redirect('404');
    }

write all the if-else statements in the controller.

Route::get('/dashboard', 'DashboardController@index')->middleware(['auth'])->name('dashboard');

In controller create a method name index and write the above code

    public function __construct(){
        $this->middleware('auth');
    }

    public function index(){
    if (Auth::user()->type === 'admin') {
            return view('adminDashboard');
        } 
elseif (Auth::user()->type === 'manager') {
           return view('ManagerDashboard')
        }
 elseif (Auth::user()->type === 'user') {
            return view('UserDashboard');
        } 
else return redirect('404');
    }
甜尕妞 2025-01-19 00:57:14

您可以使用中间件创建只能由特定角色访问的路由组,请参阅此处的文档

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文