Skip to content

Commit 449c805

Browse files
committed
handle prefix update on route level prefix
1 parent eed1fd2 commit 449c805

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Illuminate/Routing/Route.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ public function __construct($methods, $uri, $action)
146146
{
147147
$this->uri = $uri;
148148
$this->methods = (array) $methods;
149-
$this->action = $this->parseAction($action);
149+
$this->action = Arr::except($this->parseAction($action), ['prefix']);
150150

151151
if (in_array('GET', $this->methods) && ! in_array('HEAD', $this->methods)) {
152152
$this->methods[] = 'HEAD';
153153
}
154154

155-
$this->prefix($this->action['prefix'] ?? '');
155+
$this->prefix(is_array($action) ? Arr::get($action, 'prefix') : '');
156156
}
157157

158158
/**
@@ -709,11 +709,26 @@ public function getPrefix()
709709
*/
710710
public function prefix($prefix)
711711
{
712+
$this->updatePrefixOnAction($prefix);
713+
712714
$uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/');
713715

714716
return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri);
715717
}
716718

719+
/**
720+
* Update the "prefix" attribute on the action array.
721+
*
722+
* @param string $prefix
723+
* @return void
724+
*/
725+
protected function updatePrefixOnAction($prefix)
726+
{
727+
if (! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) {
728+
$this->action['prefix'] = $newPrefix;
729+
}
730+
}
731+
717732
/**
718733
* Get the URI associated with the route.
719734
*

tests/Integration/Routing/CompiledRouteCollectionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,15 @@ public function testSlashPrefixIsProperlyHandled()
408408
$this->assertSame('foo/bar', $route->uri());
409409
}
410410

411+
public function testGroupPrefixAndRoutePrefixAreProperlyHandled()
412+
{
413+
$this->routeCollection->add($this->newRoute('GET', 'foo/bar', ['uses' => 'FooController@index', 'prefix' => '{locale}'])->prefix('pre'));
414+
415+
$route = $this->collection()->getByAction('FooController@index');
416+
417+
$this->assertSame('pre/{locale}', $route->getPrefix());
418+
}
419+
411420
public function testRouteBindingsAreProperlySaved()
412421
{
413422
$this->routeCollection->add($this->newRoute('GET', 'posts/{post:slug}/show', [

0 commit comments

Comments
 (0)