How to Make a Catch-All Route in Laravel

How do I make a Catch-All Route in Laravel

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

}

How can I set a catch all route as the very last route in Laravel

Simply try to reorder the service providers in app.php like this:

'providers' => [

/*
* Laravel Framework Service Providers...
*/

Unisharp\Laravelfilemanager\LaravelFilemanagerServiceProvider::class,
...
App\Providers\RouteServiceProvider::class,
...

],

Anyway I always write RouteServiceProvider at the end of list, if not special case

Laravel: catch all routes where the path does not start with api

Instead of this

Route::get('/{any}', 'SpaController@index')->where('any', '.*');

I use

Route::get('/{any}', 'SpaController@index')->where('any', '^(?!api).*$');

I was helped by this answer.

Laravel - Using (:any?) wildcard for ALL routes?

Edit: There has been some confusion since the release of Laravel 4 regarding this topic, this answer was targeting Laravel 3.

There are a few ways to approach this.

The first method is matching (:any)/(:all?):

Route::any('(:any)/(:all?)', function($first, $rest=''){
$page = $rest ? "{$first}/{$rest}" : $first;
dd($page);
});

Not the best solution because it gets broken into multiple parameters, and for some reason (:all) doesn't work by itself (bug?)

The second solution is to use a regular expression, this is a better way then above in my opinion.

Route::any( '(.*)', function( $page ){
dd($page);
});

There is one more method, which would let you check if there are cms pages even when the route may have matched other patterns, provided those routes returned a 404. This method modifies the event listener defined in routes.php:

Event::listen('404', function() {
$page = URI::current();
// custom logic, else
return Response::error('404');
});

However, my preferred method is #2. I hope this helps. Whatever you do, make sure you define all your other routes above these catch all routes, any routes defined after will never trigger.

Laravel: Catch all routes that does not have /api/ segment

You can catch all routes where the path does not start with api

Route::any('{all}', 'AngularController@serveFrontend')->where('all', '^(?!api).*$');

Or simply leave your catchall as the last route and it'll work as expected.

Laravel - Point all route requests to single view or function

You can use variables in your route definition. This will hit on all routes.

Route::get('/{route?}', function() {
view('layout');
});

A better approach in my opinion is to look how CMS structures do it. And do it with sections of the URL and you can handle it for more customization.

Route::get('/{model?}/{action?}', function($model, $action) {
if ($model === 'user') {
return view('user', compact('action'));
}

return view('default');
});


Related Topics



Leave a reply



Submit