Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a Catch-All Route in Laravel

I need a laravel routes.php entry that will catch all traffic to a specific domain.com/premium-section of the site so that I can prompt people to become members before accessing the premium content.

like image 518
Tarek Adam Avatar asked Jan 16 '16 19:01

Tarek Adam


People also ask

What is route caching in Laravel?

Route CachingLaravel can cache your routes to make it quicker to find the code to run. Just run the command php artisan route:cache . The problem you might find with this is that you can now cache route closures therefore you will need to change any closures to call methods on controllers.


2 Answers

You could also catch 'all' by using a regex on the parameter.

Route::group(['prefix' => 'premium-section'], function () {     // other routes     ...     Route::get('{any}', function ($any) {         ...     })->where('any', '.*'); }); 

Also can catch the whole group if no routes are defined with an optional param.

Route::get('{any?}', function ($any = null) {     ... })->where('any', '.*'); 

This last one would catch 'domain.com/premium-section' as well.

like image 156
lagbox Avatar answered Sep 24 '22 19:09

lagbox


This does the trick:

Route::any('/{any}', 'MyController@myMethod')->where('any', '.*'); 
like image 32
user3260365 Avatar answered Sep 20 '22 19:09

user3260365