vendredi 23 septembre 2016

How to catch optional argument in laravel 5.2 route

I want create a general route to catch the optional lang argument in my laravel 5.2 routes.

Currently I do it with this route

//language switcher
Route::get('lang/{lang}/{suffix}', function($lang, $suffix) {
  Session::set('applocale', $lang);
  return Redirect($suffix);
});

So, if you preceed your url with an optional "/lang/[lang-code]" router will change applocale and redirect to the resource.

However I would like to make in a cleaner way with regular expressions, someting like:

//language switcher
Route::get('(es|ca|en)/{suffix}', function($lang, $suffix) {
  Session::set('applocale', $lang);
  return Redirect($suffix);
});

But don't know how..



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire