4

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

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

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 don't get it to work. How can I accomplish this?

EDIT

Found this thread: Laravel same route, different controller

But the accepted answer:

if( ! Auth::check())

Always returns false in routes.php

Community
  • 1
  • 1
Tim van Uum
  • 1,873
  • 1
  • 17
  • 36

1 Answers1

0

Maybe you could have something along these lines?

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');
        });
    }
});
Zach Robichaud
  • 183
  • 1
  • 7