Skip to content

[7.x] Allow disabling of global middleware #32404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Illuminate/Foundation/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected function sendRequestThroughRouter($request)

return (new Pipeline($this->app))
->send($request)
->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
->through($this->gatherGlobalMiddleware($request))
->then($this->dispatchToRouter());
}

Expand Down Expand Up @@ -209,6 +209,25 @@ protected function terminateMiddleware($request, $response)
}
}

/**
* Gather the glboal route middleware for the given request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function gatherGlobalMiddleware($request)
{
if ($this->app->shouldSkipMiddleware()) {
return [];
}

if ($route = $this->router->getRoutes()->match($request)) {
return array_diff($this->middleware, $route->excludedMiddleware());
}

return $this->middleware;
}

/**
* Gather the route middleware for the given request.
*
Expand Down
70 changes: 70 additions & 0 deletions tests/Integration/Http/DisablesGlobalMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Illuminate\Tests\Integration\Http;

use Closure;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Foundation\Http\Kernel as BaseKernel;
use Orchestra\Testbench\TestCase;

/**
* @group integration
*/
class DisablesGlobalMiddlewareTest extends TestCase
{
/**
* Resolve application HTTP Kernel implementation.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function resolveApplicationHttpKernel($app)
{
$app->singleton(Kernel::class, DummyKernel::class);
}

public function testItCanDisableGlobalMiddleware()
{
$action = function () {
return response('route-response');
};

$router = $this->app->make(Registrar::class);

$router->get('foo-route', $action);
$router->get('bar-route', $action)->withoutMiddleware(DummyMiddleware::class);

$this->get('foo-route')->assertSee('middleware-response');
$this->get('bar-route')->assertSee('route-response');
}
}

class DummyMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*
* @throws \Illuminate\Auth\AuthenticationException
*/
public function handle($request, Closure $next)
{
return response('middleware-response');
}
}

class DummyKernel extends BaseKernel
{
/**
* The application's middleware stack.
*
* @var array
*/
protected $middleware = [
DummyMiddleware::class,
];
}