Different controllers depending on user role - Laravel 5.1

I'm not sure if this is a good or bad practice, but I'm trying to load the same route using a different controller / method depending on the role of the user.

Tried to do some role filtering, as shown below, but not sure if this is the way:

Route::group(['before' => 'role:admin'], function() {
   Route::get('/', 'FirstController@index');
});

Route::group(['before' => 'role:editor'], function() {
   Route::get('/', 'SecondController@index');
});


Route::filter('role', function($route, $request, $value) {
   // what to do here and is this the right way?
});

But I do not work. How can i do this?

EDIT

Found this thread: Laravel same route, different controller

But the accepted answer is:

if( ! Auth::check())

Always returns false in routes.php

+4
source share
1 answer

Maybe you could do something in this direction?

Route::group(['middlware' => ['web', 'auth']], function (Router $router)
{
    /** get the logged in user here **/
    Auth::loginUsingId(2);
    $user = Auth::user();

    //you can move this to some other function but just to get the idea out i did it this way.
    if ($user->hasRole('admin'))
    {
        $router->get('test', function ()
        {
            dd('admin');
        });
    }
    elseif ($user->hasRole('owner'))
    {
        $router->get('test', function ()
        {
            dd('owner');
        });
    }
});
0
source

Source: https://habr.com/ru/post/1622715/


All Articles