I'm trying to remove the admin middleware from specific routes and create a Route::group for admin routes only but I'm not sure if I'm doing it correctly.
I'm trying to turn this:
Route::get('/admin', [
'uses' => 'PagesController@admin',
'as' => 'admin',
'middleware' => 'roles',
'roles' => 'Admin'
]);
Into this:
Route::group(['middleware' => ['roles'], 'roles' => ['admin']], function(){
Route::get('/admin', 'pagesController@admin')->name('admin');
});
So this route group is meant to only pass people who have the role of admin to the routes. I believe it is working but I really wanna check if I have made the change correctly.
Middleware:
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->user() === null){
return redirect()->route('home');
}
$actions = $request->route()->getAction();
$roles = isset($actions['roles']) ? $actions['roles'] : null;
if ($request->user()->hasAnyRole($roles) || !$roles ) {
return $next($request);
}
return redirect()->route('home');
}
}