gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:56:05 29 4
gpt4 key购买 nike

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com