Skip to content

Commit ec38179

Browse files
authored
Ability to skip middleware from resource routes (#32891)
1 parent bd36726 commit ec38179

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/Illuminate/Routing/PendingResourceRegistration.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Routing;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Traits\Macroable;
67

78
class PendingResourceRegistration
@@ -153,6 +154,21 @@ public function middleware($middleware)
153154
return $this;
154155
}
155156

157+
/**
158+
* Specify middleware that should be removed from the resource routes.
159+
*
160+
* @param array|string $middleware
161+
* @return $this|array
162+
*/
163+
public function withoutMiddleware($middleware)
164+
{
165+
$this->options['excluded_middleware'] = array_merge(
166+
(array) ($this->options['excluded_middleware'] ?? []), Arr::wrap($middleware)
167+
);
168+
169+
return $this;
170+
}
171+
156172
/**
157173
* Indicate that the resource routes should have "shallow" nesting.
158174
*

src/Illuminate/Routing/ResourceRegistrar.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ protected function getResourceAction($resource, $controller, $method, $options)
389389
$action['middleware'] = $options['middleware'];
390390
}
391391

392+
if (isset($options['excluded_middleware'])) {
393+
$action['excluded_middleware'] = $options['excluded_middleware'];
394+
}
395+
392396
return $action;
393397
}
394398

tests/Routing/RouteRegistrarTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,18 @@ public function testCanSetMiddlewareOnRegisteredResource()
513513
$this->seeMiddleware(RouteRegistrarMiddlewareStub::class);
514514
}
515515

516+
public function testResourceWithoutMiddlewareRegistration()
517+
{
518+
$this->router->resource('users', RouteRegistrarControllerStub::class)
519+
->only('index')
520+
->middleware(['one', 'two'])
521+
->withoutMiddleware('one');
522+
523+
$this->seeResponse('controller', Request::create('users', 'GET'));
524+
525+
$this->assertEquals(['one'], $this->getRoute()->excludedMiddleware());
526+
}
527+
516528
public function testCanSetRouteName()
517529
{
518530
$this->router->as('users.index')->get('users', function () {

tests/Routing/RoutingRouteTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@ public function testMiddlewareCanBeSkipped()
205205
$this->assertEquals('hello', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent());
206206
}
207207

208+
public function testMiddlewareCanBeSkippedFromResources()
209+
{
210+
$router = $this->getRouter();
211+
$router->aliasMiddleware('web', RoutingTestMiddlewareGroupTwo::class);
212+
213+
$router->resource('foo', RouteTestControllerMiddlewareGroupStub::class)
214+
->middleware('web')
215+
->withoutMiddleware(RoutingTestMiddlewareGroupTwo::class);
216+
217+
$this->assertEquals('Hello World', $router->dispatch(Request::create('foo', 'GET'))->getContent());
218+
}
219+
208220
public function testMiddlewareWorksIfControllerThrowsHttpResponseException()
209221
{
210222
// Before calling controller

0 commit comments

Comments
 (0)