How to use a dynamic variable in Laravel group routes?
You don't need to group your routes under a lang
route, you can create your own middleware and group your routes under that middleware, and use this code inside of it:
public function handle($request, Closure $next){ $lang = request->get('locale'); $currentLang = App::getLocale(); //If locale exists in the url and it's changed if($lang && $lang != $currentLang) App::setLocale($lang); //If locale doesn't exist in the url, fallback to default locale if(!$lang) App::setLocale('en'); return $next($request); });
The url should be data/store?locale=en
, ur url should always append locale
after ?
in the url, otherwise, your default locale
will be used
EDIT: Since your routes include locale
as a part of the url, and not as a request param, then I suggest you use a library to handle this for you, because you will have a lot of stuff to do before you will be able to have a working example: Laravel localization mcmara