3

I am using middleware for route groups and have three middlewares admin, teacher, and teacheradmin
Well admin is working fine but suppose I have 10 routes and all of them defined under group teacheradmin (working case for now)
but I want only 5 of those 10 routes to be accessed by middleware teacher and all 10 to be accessed by middleware teacheradmin

this is how I nested route groups

Route::group(['middleware' => 'teacheradmin'], function() {
   //defined 5 routes only accessible by teacheradmin

   Route::group(['middleware' => 'teacher'], function() {
       //defined the other routes accessible by both teacher and teacheradmin
   });
});

but the above nesting is not working, teacheradmin is not able to access the routes defined under teacher

plz I need a direction on how can I make it work

Update:
as per the answer I have defined middleware array for common routes

Route::group(['middleware' => ['teacher', 'teacheradmin']], function() {
   //defined common routes
});

and the handle methods for teh two middleware is:

teacher

public function handle($request, Closure $next)
    {
        if(Auth::check())
        {
            if(Auth::user()->user_type != 'TEACHER')
            {
                return redirect()->route('dashboard');
            }
            return $next($request);
        }
        else
        {
            return redirect('/')
                    ->withErrors('That username/password does not match, please try again !!!.');
        }
    }

teacheradmin

public function handle($request, Closure $next)
    {
        if(Auth::check())
        {
            if(Auth::user()->user_type != 'TEACHER_ADMIN')
            {
                return redirect()->route('dashboard');
            }
            return $next($request);
        }
        else
        {
            return redirect('/')
                    ->withErrors('That username/password does not match, please try again !!!.');
        }
    }

and the dashboard route goes to this method

public function Dashboard(Request $request)
    {
        $user = Auth::user();

        if($user->user_type === 'ADMIN') {
            return redirect()->route('dashboardadmin');
        } else if($user->user_type === 'TEACHER_ADMIN') {
            return redirect()->route('dashboardteacher');
        } else if($user->user_type === 'TEACHER') {
            return redirect()->route('world_selection');
        } else {
            return redirect()->route('dashboardchild');
        }
    }

now the problem I am facing is when I am on dashboard and I try to access a common route as teacheradmin then it also goes to handle of teacher hence coming back to the same page again

2 Answers 2

6

Not sure why you are nesting them. You can attach multiple middleware via array notation to a group like this:

Route::group(['middleware' => 'teacheradmin'], function() {
    //defined 5 routes only accessible by teacheradmin
});

Route::group(['middleware' => ['teacher', 'teacheradmin']], function() {
    //defined the other routes accessible by both teacher and teacheradmin
});

Update:

I think what you are trying to do can be done by using just one middleware with middleware parameters:

Route::group(['middleware' => 'role:teacheradmin'], function() {
    //defined 5 routes only accessible by teacheradmin
});

Route::group(['middleware' => 'role:teacher,teacheradmin'], function() {
    //defined the other routes accessible by both teacher and teacheradmin
});

And in the role middleware:

public function handle($request, Closure $next, ...$roles)
{
    dd($roles);
    //Do your role checking here
    return $next($request);
}

Disclaimer: ...$roles works from php 5.6 upwards.

Sign up to request clarification or add additional context in comments.

4 Comments

any specific code to be written in middleware handle, as when I try to access any route under 2nd group (common group) as teacheradmin, even then it goes to teacherMiddleware handle and hence not working
Can you update your question with your middleware code? I am not sure what you are trying to do.
updated my question, it seems that when accessing common route it goes to the handle of both middleware and hence coming back to same page that is dashboard
Sometimes it is desirable to keep routes grouped in order to not duplicate definitions such as prefix and namespace, which could be a concern.
3

As of Laravel 8 I would write it like:

Route::group(
    ['prefix' => 'v1', 'namespace' => 'Api'],
    function(Router $router){
        Route::get('/', function(){
            return "Did you forget where you placed your keys??";
        });
        Route::post('/login', [LoginController::class, 'login']); //public
        Route::get('/register', [LoginController::class, 'register']); //public
        Route::group( //protected routes group
            ['middleware' => ['auth:sanctum']], //protected via some middleware
            function () {
                Route::get('/users', [UsersController::class, 'users']);
            }
        );
    }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.