Skip to content
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

Parameters in Route::redirect() are handled differently for implicit/explicit bindings #46273

Closed
Propaganistas opened this issue Feb 25, 2023 · 3 comments · Fixed by #46281
Closed

Comments

@Propaganistas
Copy link
Contributor

Propaganistas commented Feb 25, 2023

  • Laravel Version: 10.1.3
  • PHP Version: 8.2.0
  • Database Driver & Version: MySQL 8.0

Description:

Consider the following:

// web.php
Route::redirect('user/{user}', '/user/{user}/overview');
Route::get('user/{user}/overview', UserController::class);

// app/Http/Controllers/UserController.php
class UserController
{
    public function __invoke(User $user)
    {
          // ...
    }
}

When ran as-is and hence implicitly binding the User model, the redirect works as expected. The redirect() method receives a user id and splices it into the destination URL. As a consequence the {user} parameter is only substituted in the second request (after the redirect).

When explicitly defining the binding, things change:

// app/Providers/RouteServiceProvider.php : boot()
Route::bind('user', function ($id) {
    return User::findOrFail($id); // probably extended with additional query logic.
});

The {user} parameter is now also substituted in the first request, meaning that the framework's RedirectController receives a full user model which it'll be unable to easily splice into the destination URL. Ultimately the application breaks and an exception is thrown.

Writing logic in the binding to check for redirect routes feels rather cumbersome.
I think this is a valid use case for the framework to disable the SubstituteBindings middleware in case of redirect routes... Can't think of any scenario where raw request parameters shouldn't end up in the destination url as-is.

@Propaganistas Propaganistas changed the title Parameters in Route::redirect() get resolved differently for implicit/explicit bindings Parameters in Route::redirect() are handled differently for implicit/explicit bindings Feb 26, 2023
@timacdonald
Copy link
Member

Opened a PR to fix this issue: #46281

@Propaganistas
Copy link
Contributor Author

Thanks for the quick follow-up!

@timacdonald
Copy link
Member

no sweat. thanks for reporting the issue.

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 a pull request may close this issue.

2 participants