Search code examples
phplaravellaravel-passportlaravel-middlewarelaravel-6

Function inside route group [laravel-passport]


i have some function that check something and give back in some cases "exit();". i want to use it inside Route::group. how can i do it right without it impact all the other routes? thanks!

Route::group(['middleware' => ['auth:api']], function() {
    myFunction (); //this function can give back: exit();
    Route::get('/test', 'Api\Test@test');
});

Solution

  • Turn your function into middleware: https://laravel.com/docs/5.8/middleware

    Group the routes that must be affected by your check, and leave out the routes that don't.

    Route::group(['middleware' => ['auth:api']], function() {
       Route::group(['middleware' => ['MyMiddleware']], function() {
          Route::get('/check-me', 'Api\Test@test1');
       });
       Route::get('/dont-check-me', 'Api\Test@test2');
    });