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
Get Numbers from String with PHP
PHP Simplexml Namespace Problem
Laravel: Using Try...Catch with Db::Transaction()
Rounding to Nearest Fraction (Half, Quarter, etc.)
How to Include Authorization Header in Curl Post Http Request in PHP
PHP - Your PHP Installation Appears to Be Missing the MySQL Extension Which Is Required by Wordpress
How to Set, Get and Destroy Cookies in Wordpress
Is It Ever Ok to Store Password in Plain Text in a PHP Variable or PHP Constant
Why Is Mime_Content_Type() Deprecated in PHP
PHP Simplexml: Insert Node at Certain Position
What Does Double Question Mark () Operator Mean in PHP
Calling a Particular PHP Function on Form Submit
Handle Fatal Errors in PHP Using Register_Shutdown_Function()
Setcookie() Does Not Set Cookie in Google Chrome
Ajax Post Request in Laravel 5 Return Error 500 (Internal Server Error)