php - Laravel 授权中间件 : Class can does not exist

标签 php laravel authorization laravel-5.3

我正在尝试通过中间件保护路由 described in the doc

当我点击 url 时,我得到:

ReflectionException in Container.php line 749:
Class can does not exist

这是来自 routes.php 的相关部分:

Route::get('{user}/profile/edit/{type?}', [
    'as'   => 'edit',
    'uses' => 'User\UserController@edit',
    'middleware' => ['can:edit-user,user'],
]);

AuthServiceProvider.php:

public function boot()
{
    $this->registerPolicies();

    // ... other definitions

    Gate::define('edit-user', function ($user, $subjectUser) {
        return
            $user->hasRole('manage.user') // if the user has this role, green
            ||
            ($user->isAdmin && $user->id == $subjectUser->id) // otherwise if is admin, can edit her own profile
            ;
    });

可能是因为我没有使用单独的策略类来定义门?

最佳答案

根据documentation on using Middleware with Routes - 你需要在 app/Http/Kernel.php

中注册定义

If you want a middleware to run during every HTTP request to your application, simply list the middleware class in the $middleware property of your app/Http/Kernel.php class.

您看到的错误表明缺少此定义。您需要添加类似的内容;

// Within App\Http\Kernel Class...

protected $routeMiddleware = [
    //...
    'can' => \Path\To\Your\Middleware::class,
];

关于php - Laravel 授权中间件 : Class can does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43088415/

相关文章:

php - PDO 插入不插入多个值

php - "in connector.php line 68 could not find driver"

php - UML 类图上的 Laravel MVC 应用程序结构

node.js - 使用 Express-Session 和 JWT

python - 需要一个 python 身份验证/授权框架

authentication - 考虑放弃编写自定义成员和角色提供程序的想法。意见?

php - 理解 PHP 的 mb_detect_encoding 和 mb_check_encoding 函数的结果

php - 同样的结果在 HackerRank 中看起来是错误的

javascript - 下载文件时禁止打开弹出窗口

php - Guzzle http 和 Laravel Http post 方法替换为 get 方法。 405 不允许的方法