Skip to content

Instantly share code, notes, and snippets.

@devsrv
Created June 19, 2018 12:12
Show Gist options
  • Select an option

  • Save devsrv/98ca653eb188085b1bbbc8b55a450099 to your computer and use it in GitHub Desktop.

Select an option

Save devsrv/98ca653eb188085b1bbbc8b55a450099 to your computer and use it in GitHub Desktop.
Laravel dynamic URL segment
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', '.*');
@serious-angel
Copy link

serious-angel commented Jan 19, 2026

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/foo would pass through. With the match regex, the parameter {all} becomes /login/foo (i.e. not split by /).

Meanwhile, are you sure the GET is the correct choice? What if Route::any('{all}')?

Related: Laravel subdomain routing redirect to 404 if route not in subdomain - StackOverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment