Support the ongoing development of Laravel.io →
Requests Architecture

Is this possible something like this:

if(Auth::user()->isCompany()){
    $middleware = "auth.company";
    $namespace = 'Company';
} else {
    $middleware = "auth.admin";
    $namespace = null;  
}

Route::group(['prefix' => 'admin'], 'middleware' => $middleware , 'namespace' => $namespace , function(){
     Route::get('/','AdminController@getIndex');
     Route::get('users', 'UsersController@getIndex');
});

I basically want to use the same route ( and sub-routes) but would behave different controllers handling them based on a condition. For this instance let's say a user role . I wouldn't like to put it on the same controller because it would mess up the controller with lots of conditionals.

The problem here is I couldn't access Auth, How should I go about this?

Last updated 2 years ago.
0

in laravel 5, you can access Auth::user in the middleware file...

0

Have you included:

use Illuminate\Support\Facades\Auth;
0

Sign in to participate in this thread!

Eventy

Your banner here too?

geocine geocine Joined 10 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.