Skip to content

Commit

Permalink
[9.x] Fix default parameter bug (#42942)
Browse files Browse the repository at this point in the history
* Fix default parameter bug

* wip

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
driesvints and taylorotwell authored Jun 24, 2022
1 parent aad4521 commit 4d46bda
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ class Route
*/
protected $originalParameters;

/**
* The parameters to exlude when determining the route's parameter names.
*
* @var array
*/
public $excludedParameters = [];

/**
* Indicates "trashed" models can be retrieved when resolving implicit model bindings for this route.
*
Expand Down Expand Up @@ -507,9 +514,11 @@ protected function compileParameterNames()
{
preg_match_all('/\{(.*?)\}/', $this->getDomain().$this->uri, $matches);

return array_map(function ($m) {
return array_values(array_filter(array_map(function ($m) {
return trim($m, '?');
}, $matches[1]);
}, $matches[1]), function ($parameterName) {
return ! array_key_exists($parameterName, $this->excludedParameters);
}));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ public function route($name, $parameters = [], $absolute = true)
*/
public function toRoute($route, $parameters, $absolute)
{
$route->excludedParameters = $this->getDefaultParameters();

$parameters = collect(Arr::wrap($parameters))->map(function ($value, $key) use ($route) {
return $value instanceof UrlRoutable && $route->bindingFieldFor($key)
? $value->{$route->bindingFieldFor($key)}
Expand Down
22 changes: 21 additions & 1 deletion tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,28 @@ public function testRoutableInterfaceRoutingWithCustomBindingField()
$model = new RoutableInterfaceStub;
$model->key = 'routable';

$this->assertSame('/foo/test-slug', $url->route('routable', ['bar' => $model], false));
$this->assertSame('/foo/test-slug', $url->route('routable', $model, false));
$this->assertSame('/foo/test-slug', $url->route('routable', [$model], false));
$this->assertSame('/foo/test-slug', $url->route('routable', ['bar' => $model], false));
}

public function testRoutableInterfaceRoutingWithUrlDefaults()
{
$url = new UrlGenerator(
$routes = new RouteCollection,
Request::create('http://www.foo.com/')
);

$url->defaults(['locale' => 'baz']);
$route = new Route(['GET'], '{locale}/foo/{bar:slug}', ['as' => 'routable']);
$routes->add($route);

$model = new RoutableInterfaceStub;
$model->key = 'routable';

$this->assertSame('/baz/foo/test-slug', $url->route('routable', $model, false));
$this->assertSame('/baz/foo/test-slug', $url->route('routable', [$model], false));
$this->assertSame('/baz/foo/test-slug', $url->route('routable', ['bar' => $model], false));
}

public function testRoutableInterfaceRoutingAsQueryString()
Expand Down

0 comments on commit 4d46bda

Please sign in to comment.