Created
May 13, 2019 13:33
-
-
Save wagura-maurice/4f851f748251b0caced04c6336fdf1f6 to your computer and use it in GitHub Desktop.
laravel any / all routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment