Laravel Auth 中间件:Class can 不存在

Laravel Auth Middleware: Class can does not exist

我正在尝试通过中间件保护路由 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
            ;
    });

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

根据 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,
];