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.
Asked
Active
Viewed 3.3k times
41

Stephen Ostermiller
- 23,933
- 14
- 88
- 109

Tarek Adam
- 3,387
- 3
- 27
- 52
5 Answers
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
-
3Thanks 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
-
2Thank 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
-
1just add the `?` to the parameter and will catch even the root uri ie: `{any?}` – Dreanmer Jul 09 '21 at 00:09
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
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
-
so that route will catch 'domain.com/premium-section/anywhere/they/try/to/go' ? – lagbox Jan 16 '16 at 19:44
-
-
Having the parameters as an array is handy if you need to build a redirect()->route('somwhere-else', $withParams) – Tarek Adam Jan 16 '16 at 20:23
-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