Created
June 19, 2018 12:12
-
-
Save devsrv/98ca653eb188085b1bbbc8b55a450099 to your computer and use it in GitHub Desktop.
Laravel dynamic URL segment
This file contains hidden or 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
| Route::get('{all}', function ($uri_1, $uri_2 = null, $uri_3 = null, $uri_4 = null, $uri_5 = null) { | |
| $uri = null; | |
| for ($i = 1; $i <= 5; $i++) { | |
| if (${'uri_' . $i}) { | |
| $uri .= ${'uri_' . $i} . '/'; | |
| } | |
| } | |
| $uri = rtrim($uri, '/'); | |
| return $uri; | |
| })->where('all', '.*'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! Would you still consider it a correct solution for the current Laravel version/state?
Just in case, the match regex is mandatory to also catch URL path segments. Otherwise, URL as
//sub.example.com/login/foowould pass through. With the match regex, the parameter{all}becomes/login/foo(i.e. not split by/).Meanwhile, are you sure the
GETis the correct choice? What ifRoute::any('{all}')?Related: Laravel subdomain routing redirect to 404 if route not in subdomain - StackOverflow