我在中间件auth组中保留了一些laravel路由:
Route::group(['middleware'=>'auth'],function(){
Route::controller('Activities', 'ActivitiesController');
Route::get('foo','FooController@getFoo');
.....
});
当我尝试登录访问这些页面时,我无法登录并且url一次又一次地重定向到登录页面。但如果我使用构造函数:
public function __construct()
{
$this->middleware('auth');
}
在那些控制器中它完美无缺。什么是路由组问题?
答案 0 :(得分:1)
这适用于我,路线
Route::group(['middleware'=>'auth'],function(){
Route::controller('activities', 'ActivitiesController');
});
然后是控制器
<?php namespace App\Http\Controllers;
class ActivitiesController extends Controller {
public function getIndex() {
return 'you are in;
}
}
尝试访问/活动时我被重定向到登录页面,并且成功返回到“活动”,“你在&#39;”。
答案 1 :(得分:1)
Route有一个::middleware
类,您可以使用:
路由> web.php
Route::middleware(['auth'])->group(function(){
Route::get('/activities', 'ActivitiesController@index');
});
您也可以使用我更喜欢的Route::resource();
。如果您不知道它的作用,请参阅以下文档:Image Composition Example
答案 2 :(得分:0)
在web.php中:
$roleGeneral = role1.'~'.role2.'~'.role3.'~'.role4;
Route::group(['middleware' => ['permission.role:'.$roleGeneral]], function() {})
在Kernel.php中:
protected $routeMiddleware = [...,
'permission.role' => \App\Http\Middleware\CheckPermission::class,
];
在CheckPermission.php中:
public function handle($request, Closure $next, $role)
{
$roleArr = explode('~', $role);
$token = JWTAuth::getToken();
$user = JWTAuth::toUser($token);
$roleLogin = SysRoleModel::where('id', $user->role_id)->first();
if (in_array($roleLogin['name'], $roleArr)){
return $next($request);
}else{
return \Redirect::back()->withMessage('You are not authorized to access!');
}
}