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