How to make a catch-all route in Laravel 5.2

I need a laravel 5.2 routes.php entry that catches all traffic to a specific domain.com/premium segment of the site so that I can encourage people to become members before accessing premium content.

I will answer my question ~ and hopefully people can use this information.

+8
php laravel-5 routing
source share
3 answers

You can also catch "everything" using a regular expression for a parameter.

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

You can also catch the entire group if no routes are defined with an optional parameter.

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

This last one will catch "domain.com/premium-section".

+19
source share
  • In app / Http / routes.php, I create a route that will catch all the traffic in the domain.com/premium-section/anywhere/they/try/to/go domain and try to find and execute the corresponding function in the PremiumSectionController
  • But there are no suitable methods, just tricks.

     Route::group(['as' => 'premium-section::', 'prefix' => 'premium-section', 'middleware' => ['web']], function(){ Route::any('', 'PremiumSectionController@premiumContentIndex'); Route::controller('/', 'PremiumSectionController'); }); 

.

  namespace App\Http\Controllers; use ... class PremiumSectionController extends Controller{ public function premiumContentIndex(){ return 'no extra parameters'; } //magically gets called by laravel public function missingMethod($parameters = array()){ return $parameters; } } 
+2
source share

This is the trick:

 Route::any('/{any}', 'MyController@myMethod')->where('any', '.*'); 
+1
source share

All Articles