10

Is it possible to disable a specific middleware without disabling all middleware?

I will use it when running tests, so I don't want to define middleware groups and then assign them to my routes.

$this->withoutMiddleware(); // <-- This will prevent all middleware 

$this->withoutMiddleware('web'); // <-- What I want is something like this 
aleixfabra
  • 1,075
  • 3
  • 11
  • 24

3 Answers3

5

I have an alternative solution, you could add a condition in your impacted middleware according to your environnement :

public function handle($request, Closure $next)
{
    if (App::environment('testing')) {
        return $next($request);
    }

    // Your middleware logic

    return $next($request);
}
soywod
  • 4,377
  • 3
  • 26
  • 47
  • I think this should be a good solution for me. I wish there was a method that you can disable a concrete middleware. – aleixfabra Apr 21 '16 at 08:22
  • I don't know the existence of such a function. It could be a smart pull request for the laravel repo – soywod Apr 21 '16 at 08:42
0

Well, you can use middleware groups which is a much better approach. Define a few groups, and assign them to your routes.

Kreshnik Hasanaj
  • 4,815
  • 1
  • 15
  • 18
  • 1
    Web, mentioned in the question is (likely) a middleware group already. Even so, this doesn't explain how to disable a single middleware, or middleware group. – Ben Swinburne Apr 20 '16 at 16:13
0

You may add particular routes to the $except array in the middleware itself.

For example: I didn't want my api routes to have web middleware, so this is what i did in VerifyCsrfToken.php

protected $except = [
    "api/*",
    "more/routes",
];
MD Singh
  • 1,455
  • 1
  • 9
  • 17