41

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Tarek Adam
  • 3,387
  • 3
  • 27
  • 52

5 Answers5

53

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 example.com/premium-section as well.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
lagbox
  • 48,571
  • 8
  • 72
  • 83
  • 3
    Thanks for your answer. I wonder why Laravel is not coded to work with a simple `Route:any('*')`! – Handsome Nerd Oct 14 '17 at 00:05
  • 2
    Thank you for the solution! Just a tip, It'd be better not to use Closures, because they can't be cached. ```Route::get('/{action}', 'SiteController@defaultPageHandler')->where('action', '.*');``` would be better ;) – Leonid Dashko Sep 13 '18 at 17:30
  • 2
    @LeonidDashko `Route::fallback` would be better as it is just a shortcut for exactly that. – lagbox Sep 13 '18 at 18:59
  • @lagbox I guess they can't be cached as well :) But it's good to know about this alternative. – Leonid Dashko Sep 13 '18 at 19:42
  • 1
    `Route::fallback` is not a closure, so it can be cached – lagbox Sep 13 '18 at 20:56
44

This does the trick:

Route::any('/{any}', 'MyController@myMethod')->where('any', '.*');
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
user3260365
  • 441
  • 4
  • 3
  • i always get a "too few arguments" error EDIT: you'll also need a `/` route, otherwise hitting the actually root homepage will result in a 500 error – Sliq Oct 05 '20 at 20:23
  • 1
    just add the `?` to the parameter and will catch even the root uri ie: `{any?}` – Dreanmer Jul 09 '21 at 00:09
5

Laravel now has a built-in method for this:

https://laravel.com/docs/master/routing#fallback-routes

Visti K
  • 225
  • 3
  • 4
1
  1. In app/Http/routes.php I create a route that will catch all traffic within domain.com/premium-section/anywhere/they/try/to/go and attempt to find and execute a matching function within PremiumSectionController
  2. But there aren't any matching methods, just a catch-all.

    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;
        }

    }
Tarek Adam
  • 3,387
  • 3
  • 27
  • 52
-1

This works for me

// The catch-all will match anything except the previous defined routes.
Route::any('{catchall}', 'CatchAllController@handle')->where('catchall', '.*');
Erich García
  • 1,648
  • 21
  • 30