【问题标题】:What is the best way to restrict any routes in Laravel 5.0?在 Laravel 5.0 中限制任何路由的最佳方法是什么?
【发布时间】:2015-08-14 02:32:41
【问题描述】:

我想限制我的应用程序的某些路由,并且只允许我经过身份验证的用户使用。

我尝试使用auth:check() 函数进行检查,但它似乎不起作用。

// Route Restriction
if (Auth::check()){

    //Web Directory
    Route::get('web-directory','WebDirectoryController@index');
}

当我到达mysite/web-directory 时,我仍然收到 404 错误 - 即使我目前正在登录。

在 Laravel 5.0 中限制任何路由的最佳方法是什么?

【问题讨论】:

    标签: php laravel routes laravel-5 php-5.5


    【解决方案1】:

    好的,所以我想出了解决我自己问题的方法。

    我这样做会限制我的路线

    // Route group
    $router->group(['middleware' => 'auth'], function() {
    
        //Web Directory
        Route::get('web-directory','WebDirectoryController@index');
    }
    

    现在,我可以正常走自己的路线了,只有在用户尚未登录时才会出现 404 错误。

    我希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      这也可以通过单独限制路由来实现:

      Route::get('web-directory', [
          'middleware' => 'auth', 
          'uses' => 'WebDirectoryController@index'
      ]);
      

      【讨论】:

        猜你喜欢
        • 2015-09-05
        • 2020-02-12
        • 2018-04-07
        • 1970-01-01
        • 2012-10-14
        • 2021-07-14
        • 1970-01-01
        • 1970-01-01
        • 2019-08-10
        相关资源
        最近更新 更多