logo
podcast Podcast
get help Get Unstuck

Redirect routes match all HTTP verbs

If you need to be more precise, here's a better way

Joel Clermont
Joel Clermont
2023-12-29

Route::redirect is registered as an ANY route. That means it will match all HTTP verbs, including POST, PUT, PATCH, and DELETE. This can be a problem if you're trying to redirect a GET request, but still want to use the other verbs for other routes.

If you want to do a redirect, but only for GET requests, you can use the Route::get method with a simple one-line closure, so you don't need the complexity of adding a controller and action.

// this would match all HTTP verbs
Route::redirect('users', route('some-other-route'));

// this only matches GET requests
Route::get('users', fn () => redirect()->route('some-other-route'));

In the past, I resisted using closure-based routes because it would break route caching. But Laravel has allowed for closure-based routes for a while now, so this is no longer a limitation.

Here to help,

Joel

P.S. Are you worried it would take a long time to get us up to speed on your project? We've worked with dozens of Laravel teams, so we know how to get up to speed quickly.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.