Laravel 角色中间件,未找到类角色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37260860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel RoleMiddleware, class role not found
提问by Devon
I'm trying to add a simple middleware to check if a user matches a role. I'm running into an issue when I use the middleware, I get an Exception:
我正在尝试添加一个简单的中间件来检查用户是否与角色匹配。使用中间件时遇到问题,出现异常:
ReflectionException: class role does not exist
ReflectionException: 类角色不存在
I do not attempt to call a class named role so I assume this is happening magically in Laravel somewhere.
我不会尝试调用名为 role 的类,所以我认为这在 Laravel 的某个地方神奇地发生了。
My middleware:
我的中间件:
class RoleMiddleware
{
/**
* Run the request filter.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $role
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if (! $request->user()->is($role)) {
return redirect('/login');
}
return $next($request);
}
}
In the users table, I have a role field and in the User model I have:
在用户表中,我有一个角色字段,在用户模型中我有:
/**
* Check if a user is a certain role
*
* @param $role
* @return bool
*/
function is($role) {
return ($this->role == $role);
}
The route group:
路线组:
Route::group(['prefix' => 'support', 'middleware' => ['role:admin', 'web']], function() {
Route::get('threads', 'ThreadController@index');
});
In Http/Kernel.php:
在 Http/Kernel.php 中:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'role' => [
RoleMiddleware::class,
],
];
Anyone have any ideas on what could be causing this ReflectionException?
任何人都对可能导致此 ReflectionException 的原因有任何想法?
采纳答案by Devon
This came down to two problems:
这归结为两个问题:
$middlewareGroups
may not allow for parameters. (Needs to be confirmed) DroppingRoleMiddleware
down to$routeMiddleware
got rid of the exception.Having 'web' after 'role:admin' resulted in a
null
$request->user()
. So for future users, you may need to consider placement of your middleware and a check to see if$request->user()
is null.
$middlewareGroups
可能不允许参数。(需要确认)RoleMiddleware
下拉$routeMiddleware
去掉异常。在 'role:admin' 之后加上 'web' 会导致
null
$request->user()
. 因此,对于未来的用户,您可能需要考虑放置中间件并检查是否$request->user()
为空。
Hope this helps someone else.
希望这对其他人有帮助。
回答by Randika Vishman
For Spatie/Laravel-Permissions to work properly we have to register the two route middleware (role
and permission
) in app/Http/Kernel.php
as follows, along with the other two auth middleware:
为了让 Spatie/Laravel-Permissions 正常工作,我们必须按如下方式注册两个路由中间件 (role
和permission
) app/Http/Kernel.php
,以及另外两个 auth 中间件:
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
];
回答by Niraj Shah
In Http/Kernel.php
, you need to include the full path to RoleMiddleware
. E.g.:
在 中Http/Kernel.php
,您需要包含到RoleMiddleware
. 例如:
...
'role' => [
\App\Http\Middleware\RoleMiddleware::class,
],
...
回答by Vipertecpro
Try to change this
尝试改变这一点
Route::group(['prefix' => 'support', 'middleware' => ['role:admin', 'web']], function() {
Route::get('threads', 'ThreadController@index');
});
to
到
Route::group(['prefix' => 'support', 'middlewareGroups' => ['role:admin', 'web']], function() {
Route::get('threads', 'ThreadController@index');
});
I'm a beginner in laravel i was facing this same problem and fixed by changing the name from middleware to "your-own-middelware-name".
我是 laravel 的初学者,我遇到了同样的问题,并通过将名称从中间件更改为“您自己的中间件名称”来解决。