PKarag's avatar

Route handling best practice

Hello, im curious as to how would anyone solve this routing example and whats the best practice. Essentially i would like only the post method to be available and every other method to redirect to home or maybe throw a 404. My solutions is the following but im not sure its the correct one.

Route::any('/myRoute/myAction', [MyController::class, 'redirectHome']);
Route::post('/myRoute/myAction', [MyController::class, 'myAction']);
0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You could just add a middleware to the any route that redirects if the method is wrong

bugsysha's avatar

More specific routes should be listed first, then more generic ones. So in your case you need to reverse them.

PKarag's avatar

@orest It actually doesn't redirect always to home and works with that ordering.

Please or to participate in this conversation.