PHP 工匠路由:列出门错误


php artisan route:list error with gate

所以,我将 Gate 外观添加到我的用户控制器中的构造函数中

public function __construct()
{
    if (Gate::denies('manage-user')) {
        abort(404);
    }
}

一切都按预期工作,但有一件事,现在php artisan route:list显示以下错误

$ php artisan route:list
[Symfony'Component'HttpKernel'Exception'NotFoundHttpException]

如果我移除门,php artisan route:list运行良好。有人知道为什么会这样吗?以及如何解决它?工匠可以绕过大门立面吗?

我认为您要避免在控制器构造函数中进行此类检查。Laravel文档展示了许多实现授权检查的方法,这些方法都不在控制器构造函数中。

https://laravel.com/docs/5.2/authorization#checking-abilities

我会亲自创建一个 FormRequest,并使用执行检查的授权方法。然后,将该 FormRequest 注入到每个方法中,它将自动运行授权。

https://laravel.com/docs/5.2/authorization#within-form-requests

https://laravel.com/docs/5.2/validation#form-request-validation

我使用了这个命令

public function __construct()
{
    // check if request not from cli
    if ('cli' != php_sapi_name()) {
        $this->authorize('is_admin');
    }
}