Skip to content

Commit 1b1decb

Browse files
[8.x] Fixes middleware() not accepting Stringable (#39449)
* Fixes `middleware()` not accepting Stringable. * Reverses the string to array check. * Removed redundant not-array checks for middleware attribute. * Forces the middleware registration as array.
1 parent d37d032 commit 1b1decb

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

src/Illuminate/Routing/PendingResourceRegistration.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,10 @@ public function parameter($previous, $new)
149149
*/
150150
public function middleware($middleware)
151151
{
152-
if (! is_string($middleware)) {
153-
$middleware = Arr::wrap($middleware);
152+
$middleware = Arr::wrap($middleware);
154153

155-
foreach ($middleware as $key => $value) {
156-
$middleware[$key] = (string) $value;
157-
}
154+
foreach ($middleware as $key => $value) {
155+
$middleware[$key] = (string) $value;
158156
}
159157

160158
$this->options['middleware'] = $middleware;

src/Illuminate/Routing/Route.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,10 +1025,14 @@ public function middleware($middleware = null)
10251025
return (array) ($this->action['middleware'] ?? []);
10261026
}
10271027

1028-
if (is_string($middleware)) {
1028+
if (! is_array($middleware)) {
10291029
$middleware = func_get_args();
10301030
}
10311031

1032+
foreach ($middleware as $index => $value) {
1033+
$middleware[$index] = (string) $value;
1034+
}
1035+
10321036
$this->action['middleware'] = array_merge(
10331037
(array) ($this->action['middleware'] ?? []), $middleware
10341038
);

src/Illuminate/Routing/RouteRegistrar.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ public function attribute($key, $value)
9393
throw new InvalidArgumentException("Attribute [{$key}] does not exist.");
9494
}
9595

96-
if ($key === 'middleware' && ! is_string($value)) {
97-
$value = Arr::wrap($value);
98-
96+
if ($key === 'middleware') {
9997
foreach ($value as $index => $middleware) {
10098
$value[$index] = (string) $middleware;
10199
}

tests/Routing/RouteRegistrarTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ public function __toString()
7979
$this->assertSame(['one'], $this->getRoute()->middleware());
8080
}
8181

82+
public function testMiddlewareAsStringableObjectOnRouteInstance()
83+
{
84+
$one = new class implements Stringable
85+
{
86+
public function __toString()
87+
{
88+
return 'one';
89+
}
90+
};
91+
92+
$this->router->get('users', function () {
93+
return 'all-users';
94+
})->middleware($one);
95+
96+
$this->seeResponse('all-users', Request::create('users', 'GET'));
97+
$this->assertSame(['one'], $this->getRoute()->middleware());
98+
}
99+
82100
public function testMiddlewareAsArrayWithStringables()
83101
{
84102
$one = new class implements Stringable

0 commit comments

Comments
 (0)