Laravel - Using (:Any) Wildcard for All Routes

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 5.4 - How to route with wildcard?

You can do this using a route parameter that allows any value. E.g. (.*). You'll then need to handle all of the requests from a single controller action.

Route::get('members/{action}', 'MemberController@all')->where('action', '(.*)');

To take this further, if you don't know that your requests will all be GET requests, you could use the :any method type.

Route::any('members/{action}', 'MemberController@all')->where('action', '(.*));

Laravel routes with wildcards always 404

For any other new people to Laravel, this is how you set up a route with an optional parameter.

Route::get('hello/(:any?)', function ($name = 'default') {
return "Welcome, $name.";
});

Laravel Routes Issues when using wildcards

Try this

Route::any('/{slug}', function($slug) {
$page = App\Models\Page::firstByAttributes(['slug' => $slug]);
if($page) {
return Redirect::action('pagesController@find', array($page));
}
})->where('slug', '.*');

You can also consider using route filters to achieve this in a slightly more readable way.

Wildcard URL routing in Laravel

You may try something like this but probably not a very good solution:

Other route declaration
Route::get('')

// At the bottom
Route::get('{result}/{any?}', function($result, $any = null) {

// $any is optional
if($any) {
$paramsArray = explode('/', $any);
// Use $paramsArray array for other parameters
}

})->where('any', '(.*)');

Be careful, it can catch any URL that matches with this. Put this at the bottom of all routes.

Wildcard to route to a controller

Yes, with routes in laravel the format is this:

Route::get('/custom-controller', 'CustomController@index');

If you are using the standard method names in your controller such as index(), store() etc

you can use resource instead and it will automatically build up all your routes:

Route::resource('/my-route', 'CustomController');

If you then do php artisan route:list you will see laravel has automatically added all the standard routes for your controller.

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: 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.



Related Topics



Leave a reply



Submit