-
Notifications
You must be signed in to change notification settings - Fork 11.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
URL::defaults() are ignored in UrlGenerator::toRoute() #54796
Comments
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
I'm going over some hypothetical scenarios to see what'd be the ideal implementation here and this makes the most sense to me:
In the following examples
Which would look something like:
* Assuming mixed arrays are supported, we should probably treat named params similarly to defaults in how we remove them from the list used for transforming positional parameters. However unlike defaults, named params also get transformed and included in the final array passed to the There may be an elegant way to implement this that I'm not seeing, but I think unfortunately the fix will make the method a lot uglier. |
While trying to reproduce this in laravel/framework tests, I ran into some seemingly different issues, likely related to the objects used in those tests, will need to investigate further. However that made me try to reproduce this in just a fresh Laravel app and while testing that I thought that even the behavior I'm trying to fix here wouldn't actually be correct. If we define I think
That would make defaults actually correctly usable with binding fields. @crynobone can I get some feedback on this? The current behavior is broken in several ways, I'm trying to fix one of those broken things, but taking a step back it wouldn't make sense anyway so I think first I'd need to know if you'd like the |
Another case: Route::get('/test/{foo}/{user}', fn () => 'foo')->name('abc');
URL::defaults(['foo' => 'bar']);
$user = User::first();
// http://foo.com/test/bar/bar?1
dd(route('abc', ['bar', $user]));
// http://foo.com/test/bar/1
dd(route('abc', ['foo' => 'bar', 'user' => $user])); Here If the I think the handling of defaults should be rewritten overall a bit. Luckily it's not too much logic so I think this should be feasible without any real breaking changes. |
Laravel Version
12
PHP Version
any
Database Driver & Version
irrelevant
Description
Given:
Route::get('/{foo:slug}/example/{bar}', ...)->name('example');
URL::defaults(['foo' => 'some_value']);
route('example', $bar)
will produce a "Missing required parameter" exception.On the other hand,
route('example', ['bar' => $bar])
will work fine.The issue comes from
UrlGenerator::toRoute()
:The default parameter only gets used in
$this->routeUrl()
(RouteUrlGenerator
), but by that point theUrlGenerator
has broken the provided parameters, passing a[null]
array.I think the solution would be something along the lines of:Actually probably a bit more complex than this because defaults can work for latter parameters too, not just the earliest ones. I'll try to send a PR with some reasonable implementation.$key
is numeric, aka we've passedroute('example', $bar)
notroute('example', ['bar' => $bar])
array_key_exists($route->parameterNames()[$key], $this->getDefaultParameters())
[$key]
parameter and move on to the next oneSo in this case
$bar
should be matched against the second parameter, not the first one.Steps To Reproduce
See above
The text was updated successfully, but these errors were encountered: