Reputation: 901
What I want is I can use different controller for same route based on logged in user's role, so if user logged in with role of admin I want controller for given url is loaded from Admin namespace. I've done like this
Route::group(['middleware'=>['install','auth']],function(){
$role = \Auth::user()->role;
switch ($role)
{
case 'admin':
$prefix = 'Admin\\';
break;
case 'Foo':
$prefix = 'Foo\\';
break;
}
Route::resource('/foo',$prefix.'FooController');
//.......
But is says that Auth::user() is null, is there another approach to do this?
Upvotes: 0
Views: 1211
Reputation: 301
You can do a redirect using middleware for either the admin or the 'Foo', just check the auth role and return a redirect, to the correct route.
class AdminOnly
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::user()->role != 'admin'){
return redirect('path/to/non/admin/route');
}
return $next($request);
}
}
Then define both routes in your routes file and use the middleware to redirect between them.
Upvotes: 0
Reputation: 1466
Try auth()->user()
or include a specific Auth
Module instead of \Auth::user()
it might be that the interpreter is using a wrong Auth
module since there are quite a few of these.
Upvotes: 1