Skip to content

Commit b38e179

Browse files
committed
tweak route construction so uri is parsed with prefix and accounts for bindings
1 parent b97519f commit b38e179

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

Diff for: src/Illuminate/Routing/Route.php

+19-21
Original file line numberDiff line numberDiff line change
@@ -144,30 +144,15 @@ class Route
144144
*/
145145
public function __construct($methods, $uri, $action)
146146
{
147-
$this->uri = $this->parseUri($uri);
147+
$this->uri = $uri;
148148
$this->methods = (array) $methods;
149149
$this->action = $this->parseAction($action);
150150

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

155-
if (isset($this->action['prefix'])) {
156-
$this->prefix($this->action['prefix']);
157-
}
158-
}
159-
160-
/**
161-
* Parse the route URI and normalize / store any implicit binding fields.
162-
*
163-
* @param string $uri
164-
* @return string
165-
*/
166-
protected function parseUri($uri)
167-
{
168-
return tap(RouteUri::parse($uri), function ($uri) {
169-
$this->bindingFields = $uri->bindingFields;
170-
})->uri;
155+
$this->prefix($this->action['prefix'] ?? '');
171156
}
172157

173158
/**
@@ -703,9 +688,7 @@ public function prefix($prefix)
703688
{
704689
$uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/');
705690

706-
$this->uri = trim($uri, '/');
707-
708-
return $this;
691+
return $this->setUri(trim($uri, '/'));
709692
}
710693

711694
/**
@@ -726,11 +709,26 @@ public function uri()
726709
*/
727710
public function setUri($uri)
728711
{
729-
$this->uri = $uri;
712+
$this->uri = $this->parseUri($uri);
730713

731714
return $this;
732715
}
733716

717+
/**
718+
* Parse the route URI and normalize / store any implicit binding fields.
719+
*
720+
* @param string $uri
721+
* @return string
722+
*/
723+
protected function parseUri($uri)
724+
{
725+
$this->bindingFields = [];
726+
727+
return tap(RouteUri::parse($uri), function ($uri) {
728+
$this->bindingFields = $uri->bindingFields;
729+
})->uri;
730+
}
731+
734732
/**
735733
* Get the name of the route instance.
736734
*

Diff for: tests/Routing/RoutingRouteTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,15 @@ public function testWherePatternsProperlyFilter()
790790
$this->assertFalse($route->matches($request));
791791
}
792792

793+
public function testRoutePrefixParameterParsing()
794+
{
795+
$route = new Route('GET', '/foo', ['prefix' => 'profiles/{user:username}/portfolios', 'uses' => function () {
796+
//
797+
}]);
798+
799+
$this->assertEquals('profiles/{user}/portfolios/foo', $route->uri());
800+
}
801+
793802
public function testDotDoesNotMatchEverything()
794803
{
795804
$route = new Route('GET', 'images/{id}.{ext}', function () {

0 commit comments

Comments
 (0)