【问题标题】:Laravel 5.2 authentication doesn't work with implicit routes under prefix groupLaravel 5.2 身份验证不适用于前缀组下的隐式路由
【发布时间】:2016-07-04 14:01:40
【问题描述】:

我已经使用 make:auth 命令创建了认证系统,并且运行良好

我已经创建了另一个 Restful 控制器,并且我一直在 routes.php 上使用 Route::controller

喜欢这段代码,请阅读代码中的 // cmets

Route::group(['prefix' => 'dashboard', 'middleware' => 'web'], function () {
    Route::auth();
    Route::get('/', 'HomeController@index'); // working fine and requires logging in
    Route::get('test', 'HomeController@index'); // working fine and requires logging in
    Route::controller('account','accountController'); // doesn't work and I can visit this page without logging in
});

我看到隐式路由不能与中间件一起正常工作,但我不知道合适的解决方案

【问题讨论】:

  • 对于要进行身份验证的页面,您需要使用“auth”中间件而不是“web”。

标签: php laravel laravel-5 laravel-routing laravel-5.2


【解决方案1】:

您实际上需要auth 中间件而不是网络来进行身份验证:

Route::get('profile', ['prefix' => 'dashboard', 'middleware' => 'auth'], function() {
     Route::controller('account','accountController');
}]);

另一个选项是在控制器中设置它。

class accountController extends Controller
{
    /**
     * Instantiate a new accountController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

  ...
  ...
}

【讨论】:

  • 我试过你的解决方案,浏览器很好地重定向到日志页面,但我得到“页面没有正确重定向”,并且页面根本没有工作
  • 通过添加 $this->middleware('auth'); 我发现您的答案是解决问题的最佳方法;在控制器文件的构造函数中,根本不更改我的路由文件
  • 没问题。很高兴这很有帮助。您可以使用“php artisan route:list”获取列表,以查看每条路由及其中间件。这对于查看它们是否重叠或丢失非常有帮助。
【解决方案2】:

谢谢 Can,解决方法是在控制器文件中添加此代码

class accountController extends Controller
{
    /**
     * Instantiate a new accountController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

  ...
  ...
}

【讨论】:

    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    相关资源
    最近更新 更多