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

[9.x] Add method to remove a middleware from a group #44780

Merged
merged 5 commits into from
Oct 31, 2022
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
26 changes: 26 additions & 0 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,32 @@ public function pushMiddlewareToGroup($group, $middleware)
return $this;
}

/**
* Remove the given middleware from the specified group.
*
* @param string $group
* @param string $middleware
* @return $this
*/
public function removeMiddlewareFromGroup($group, $middleware)
{
if (! $this->hasMiddlewareGroup($group)) {
return $this;
}

$reversedMiddlewaresArray = array_flip($this->middlewareGroups[$group]);

if (! array_key_exists($middleware, $reversedMiddlewaresArray)) {
return $this;
}

$middlewareKey = $reversedMiddlewaresArray[$middleware];

unset($this->middlewareGroups[$group][$middlewareKey]);

return $this;
}

/**
* Flush the router's middleware groups.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,54 @@ public function testCanSetRouteNameUsingNameAlias()
$this->assertSame('users.index', $this->getRoute()->getName());
}

public function testPushMiddlewareToGroup()
{
$this->router->middlewareGroup('web', []);
$this->router->pushMiddlewareToGroup('web', 'test-middleware');

$this->assertEquals(['test-middleware'], $this->router->getMiddlewareGroups()['web']);
}

public function testPushMiddlewareToGroupUnregisteredGroup()
{
$this->router->pushMiddlewareToGroup('web', 'test-middleware');

$this->assertEquals(['test-middleware'], $this->router->getMiddlewareGroups()['web']);
}

public function testPushMiddlewareToGroupDuplicatedMiddleware()
{
$this->router->pushMiddlewareToGroup('web', 'test-middleware');
$this->router->pushMiddlewareToGroup('web', 'test-middleware');

$this->assertEquals(['test-middleware'], $this->router->getMiddlewareGroups()['web']);
}

public function testCanRemoveMiddlewareFromGroup()
{
$this->router->pushMiddlewareToGroup('web', 'test-middleware');

$this->router->removeMiddlewareFromGroup('web', 'test-middleware');

$this->assertEquals([], $this->router->getMiddlewareGroups()['web']);
}

public function testCanRemoveMiddlewareFromGroupNotUnregisteredMiddleware()
{
$this->router->middlewareGroup('web', []);

$this->router->removeMiddlewareFromGroup('web', 'different-test-middleware');

$this->assertEquals([], $this->router->getMiddlewareGroups()['web']);
}

public function testCanRemoveMiddlewareFromGroupUnregisteredGroup()
{
$this->router->removeMiddlewareFromGroup('web', ['test-middleware']);

$this->assertEquals([], $this->router->getMiddlewareGroups());
}

/**
* Get the last route registered with the router.
*
Expand Down