Search code examples
phplaravelroutesmiddleware

Laravel 8 same route multiple middleware using OR condition


Hello i tried to use middleware like the code below to validate OR operator

Route::get('/page', [Controller::class, 'index'])->middleware(['mid1','mid2']);

in this example it uses the AND operator not OR I used also groups like this

Route::group(['middleware' => 'mid1'], function () {
   Route::get('/page', [Controller::class, 'index']);
});
Route::group(['middleware' => 'mid2'], function () {
   Route::get('/page', [Controller::class, 'index']);
});

but using groups with same route the second route in the group is the only one readable. Any help please


Solution

  • There's nothing builtin in laravel to do such a thing.

    You can create another middle-ware to contain both conditions you need to apply.

    In you middleware:

    public function handle($request, Closure $next) {
        if (condition1 || condition2) {
           return $request($next);
        }
        abort('statusCode');
    }