Skip to content

Commit aa6538e

Browse files
authored
Fix handling of default values for route parameters with a binding field, where a default is not present for that same parameter without a binding field (#55697)
1 parent 96b554c commit aa6538e

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Illuminate/Routing/RouteUrlGenerator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,14 @@ protected function formatParameters(Route $route, $parameters)
197197
unset($parameters[$name]);
198198

199199
continue;
200-
} elseif (! isset($this->defaultParameters[$name]) && ! isset($optionalParameters[$name])) {
201-
// No named parameter or default value for a required parameter, try to match to positional parameter below...
202-
array_push($requiredRouteParametersWithoutDefaultsOrNamedParameters, $name);
200+
} else {
201+
$bindingField = $route->bindingFieldFor($name);
202+
$defaultParameterKey = $bindingField ? "$name:$bindingField" : $name;
203+
204+
if (! isset($this->defaultParameters[$defaultParameterKey]) && ! isset($optionalParameters[$name])) {
205+
// No named parameter or default value for a required parameter, try to match to positional parameter below...
206+
array_push($requiredRouteParametersWithoutDefaultsOrNamedParameters, $name);
207+
}
203208
}
204209

205210
$namedParameters[$name] = '';

tests/Routing/RoutingUrlGeneratorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,24 @@ public function testComplexRouteGenerationWithDefaultsAndBindingFields()
10661066
$url->route('tenantSlugPost', ['post' => $keyParam('concretePost')]),
10671067
);
10681068

1069+
// Repeat the two assertions above without the 'tenant' default (without slug)
1070+
$url->defaults(['tenant' => null]);
1071+
1072+
// tenantSlugPost: Tenant (with default) omitted, post passed positionally, with the default value for 'tenant' (without slug) removed
1073+
$this->assertSame(
1074+
'https://www.foo.com/tenantSlugPost/defaultTenantSlug/concretePost',
1075+
$url->route('tenantSlugPost', [$keyParam('concretePost')]),
1076+
);
1077+
1078+
// tenantSlugPost: Tenant (with default) omitted, post passed using key, with the default value for 'tenant' (without slug) removed
1079+
$this->assertSame(
1080+
'https://www.foo.com/tenantSlugPost/defaultTenantSlug/concretePost',
1081+
$url->route('tenantSlugPost', ['post' => $keyParam('concretePost')]),
1082+
);
1083+
1084+
// Revert the default value for the tenant parameter back
1085+
$url->defaults(['tenant' => 'defaultTenant']);
1086+
10691087
/**
10701088
* One parameter with a default value, one without a default value.
10711089
*

0 commit comments

Comments
 (0)