Skip to content
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

[7.x] Ability to skip a middleware from route registration #32347

Merged
merged 3 commits into from
Apr 13, 2020
Merged
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
23 changes: 23 additions & 0 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,29 @@ public function middleware($middleware = null)
return $this;
}

/**
* Set which middleware(s) to skip.
*
* @param array|string|null $middleware
* @return $this|array
*/
public function skipMiddleware($middleware = null)
{
if (is_null($middleware)) {
return (array) ($this->action['skip_middleware'] ?? []);
}

if (is_string($middleware)) {
$middleware = func_get_args();
}

$this->action['skip_middleware'] = array_merge(
(array) ($this->action['skip_middleware'] ?? []), $middleware
);

return $this;
}

/**
* Get the middleware for the route's controller.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,9 @@ public function gatherRouteMiddleware(Route $route)
{
$middleware = collect($route->gatherMiddleware())->map(function ($name) {
return (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups);
})->flatten();
})->flatten()->reject(function ($name) use ($route) {
return in_array($name, $route->skipMiddleware(), true);
});

return $this->sortMiddleware($middleware);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public function testMiddlewareFluentRegistration()
$this->assertEquals(['seven'], $this->getRoute()->middleware());
}

public function testSkipMiddlewareRegistration()
{
$this->router->middleware(['one', 'two'])->get('users', function () {
return 'all-users';
})->skipMiddleware('one');

$this->seeResponse('all-users', Request::create('users', 'GET'));

$this->assertEquals(['one'], $this->getRoute()->skipMiddleware());
}

public function testCanRegisterGetRouteWithClosureAction()
{
$this->router->middleware('get-middleware')->get('users', function () {
Expand Down
12 changes: 12 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ public function testClosureMiddleware()
$this->assertSame('caught', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent());
}

public function testMiddlewareCanBeSkipped()
{
$router = $this->getRouter();
$router->aliasMiddleware('web', RoutingTestMiddlewareGroupTwo::class);

$router->get('foo/bar', ['middleware' => 'web', function () {
return 'hello';
}])->skipMiddleware(RoutingTestMiddlewareGroupTwo::class);

$this->assertEquals('hello', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent());
}

public function testMiddlewareWorksIfControllerThrowsHttpResponseException()
{
// Before calling controller
Expand Down