Skip to content

Commit

Permalink
handle prefix update on route level prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 16, 2020
1 parent eed1fd2 commit 449c805
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Illuminate/Routing/Route.php
Expand Up @@ -146,13 +146,13 @@ public function __construct($methods, $uri, $action)
{
$this->uri = $uri;
$this->methods = (array) $methods;
$this->action = $this->parseAction($action);
$this->action = Arr::except($this->parseAction($action), ['prefix']);

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

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

/**
Expand Down Expand Up @@ -709,11 +709,26 @@ public function getPrefix()
*/
public function prefix($prefix)
{
$this->updatePrefixOnAction($prefix);

$uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/');

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

/**
* Update the "prefix" attribute on the action array.
*
* @param string $prefix
* @return void
*/
protected function updatePrefixOnAction($prefix)
{
if (! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) {
$this->action['prefix'] = $newPrefix;
}
}

/**
* Get the URI associated with the route.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Integration/Routing/CompiledRouteCollectionTest.php
Expand Up @@ -408,6 +408,15 @@ public function testSlashPrefixIsProperlyHandled()
$this->assertSame('foo/bar', $route->uri());
}

public function testGroupPrefixAndRoutePrefixAreProperlyHandled()
{
$this->routeCollection->add($this->newRoute('GET', 'foo/bar', ['uses' => 'FooController@index', 'prefix' => '{locale}'])->prefix('pre'));

$route = $this->collection()->getByAction('FooController@index');

$this->assertSame('pre/{locale}', $route->getPrefix());
}

public function testRouteBindingsAreProperlySaved()
{
$this->routeCollection->add($this->newRoute('GET', 'posts/{post:slug}/show', [
Expand Down

0 comments on commit 449c805

Please sign in to comment.