【问题标题】:php artisan route:list error with gatephp artisan route:门的列表错误
【发布时间】:2016-06-09 02:21:59
【问题描述】:

所以,我将 Gate 外观添加到我的 UserController 中的构造函数

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 运行良好。有谁知道为什么会这样?以及如何解决?工匠可以绕过大门门面吗?

【问题讨论】:

  • 你说得对,我只是将检查从构造函数移到我的方法,并且 php artisan 路由列表有效。那么,如果对构造函数进行检查是错误的,那么对整个控制器进行门检查的最佳做法是什么(我只想对我的用户控制器上的所有方法使用 1 个门,管理用户)?我是否必须手动检查所有方法?这会违反 DRY 原则吗?
  • 感谢您的建议,请将其写为答案,以便我选择它作为正确答案

标签: php laravel laravel-artisan


【解决方案1】:

我认为您希望避免在控制器构造函数中进行这样的检查。 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

【讨论】:

    【解决方案2】:

    我用过这个命令

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-27
      • 2016-05-14
      • 2017-05-16
      • 2016-10-14
      • 2017-03-20
      • 2017-12-22
      • 2016-01-29
      • 2021-03-31
      相关资源
      最近更新 更多