Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

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

From our daily Laravel tips email. Get the next one in your inbox.

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.

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.