Skip to content

Conversation

alaminfirdows
Copy link

Description:
This PR fixes an issue where URL::defaults() combined with route parameters (including model key binding fields) causes positional parameter misalignment when generating URLs with route() call.

Problem:
When a route has a parameter in the URL, e.g., {workspace:uuid?}, and its value is set via URL::defaults() (without using the route key), calling route() with positional parameters for other route parameters can incorrectly assign the first positional parameter to the prefix parameter. This results in an empty query string like ?workspace= instead of the intended URL.

Reference: #56604

Solution:
Added the check for both named parameters and key binding fields.

Example:

// Route with model key binding
Route::get('{workspace:uuid?}/customers/{customer}', [CustomerController::class, 'show'])
    ->name('customers.show');

// Middleware sets default workspace
URL::defaults(['workspace' => $workspace->uuid]);

// Route call with positional parameter
route('customers.show', $customer->id);

// ❌ Generates:
// http://laravel.test/7b1c3cbe-675e-43cc-aed6-55bc36e7f79a/customers/123?workspace=

// ✅ Should generate:
// http://laravel.test/7b1c3cbe-675e-43cc-aed6-55bc36e7f79a/customers/123

@alaminfirdows
Copy link
Author

@crynobone Could you take a quick look at this?

If everything looks good, I’d really appreciate it if you could also add the hacktoberfest-accepted label to this PR.

@taylorotwell
Copy link
Member

Hey there - there are no tests so I can't be sure if anything is fixed. 😅

@alaminfirdows
Copy link
Author

My bad! I’ll add some tests and submit another PR.

Thanks a lot for the quick feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants