Skip to content

Commit

Permalink
Ability to skip middleware from resource routes (#32891)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebdesign authored May 19, 2020
1 parent bd36726 commit ec38179
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Illuminate/Routing/PendingResourceRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Routing;

use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;

class PendingResourceRegistration
Expand Down Expand Up @@ -153,6 +154,21 @@ public function middleware($middleware)
return $this;
}

/**
* Specify middleware that should be removed from the resource routes.
*
* @param array|string $middleware
* @return $this|array
*/
public function withoutMiddleware($middleware)
{
$this->options['excluded_middleware'] = array_merge(
(array) ($this->options['excluded_middleware'] ?? []), Arr::wrap($middleware)
);

return $this;
}

/**
* Indicate that the resource routes should have "shallow" nesting.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Routing/ResourceRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ protected function getResourceAction($resource, $controller, $method, $options)
$action['middleware'] = $options['middleware'];
}

if (isset($options['excluded_middleware'])) {
$action['excluded_middleware'] = $options['excluded_middleware'];
}

return $action;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@ public function testCanSetMiddlewareOnRegisteredResource()
$this->seeMiddleware(RouteRegistrarMiddlewareStub::class);
}

public function testResourceWithoutMiddlewareRegistration()
{
$this->router->resource('users', RouteRegistrarControllerStub::class)
->only('index')
->middleware(['one', 'two'])
->withoutMiddleware('one');

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

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

public function testCanSetRouteName()
{
$this->router->as('users.index')->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 @@ -205,6 +205,18 @@ public function testMiddlewareCanBeSkipped()
$this->assertEquals('hello', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent());
}

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

$router->resource('foo', RouteTestControllerMiddlewareGroupStub::class)
->middleware('web')
->withoutMiddleware(RoutingTestMiddlewareGroupTwo::class);

$this->assertEquals('Hello World', $router->dispatch(Request::create('foo', 'GET'))->getContent());
}

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

0 comments on commit ec38179

Please sign in to comment.