简体   繁体   中英

Protect routes with middleware Laravel

I have implemented middleware roles and permissions control in my app, but I cannot figure out why it only allows me to define one '/' route. The second one is still pointing to '/home' even though I override AuthController redirectTo variable.

My routes:

Route::group(['middleware' => 'role:user'], function()
{
 Route::get('/', 'ScoresController@user');

});

Route::group(['middleware' => 'role:admin'], function()
{
Route::get('/', 'PagesController@home');
});

In any case after authentication user with user role redirecting to '/home'.

Like Simon says your second route will override the first one what you could do is load another controller wich redirects you to another page via redirect() or write it as a route itself.

Could look like this:

Route::get('/', function(){
    $user = Auth::user();

    if($user->is('admin') {
        return redirect('admin');
    }else{
        return redirect('user');
    }
});

Route::get('admin', ['middleware' => 'role:admin', 'uses'=> 'PagesController@home']);

This is just one of many possibilities hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM