How do I make a Catch-All Route in Laravel
- 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;
}
}
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
Syntax Error, Unexpected T_Encapsed_And_Whitespace, Expecting T_String or T_Variable or T_Num_String
Laravel and View Caching in Development -- Can't See Changes Right Away
How to Set a Maximum Size Limit to PHP Curl Downloads
Do PHP Pdo Prepared Statements Need to Be Escaped
Sending Correct File Size with PHP Download Script
Google Calendar API Service Account Error
Finding Free Blocks of Time in MySQL and PHP
PHP .Htaccess -> Pretty Url (In Reverse)
How to Trust PHP _Destruct() Method to Be Called
PHP Get All Arguments as Array
PHP Adding Custom Namespace Using Autoloader from Composer
Does PHP Optimize Tail Recursion