I want to add a localization information to my routes.
Currently my route definitions are:
Route::controller('browse');
Route::controller('search');
Route::controller('support');
Route::controller('filter');
I want to change URLs from /url/browse
to /url/en/browse
etc.
If language is missing from the URL then the application should redirect to the same route with default language. This means accessing the old /url/browse
would redirect to /url/default/browse
.
I tried to make or find some simple solution with filters, unsuccessfully. Thanks for help!
Try to use the Default Application Language and Supported Languages array in config.
If you put a language in the supported languages any route starting with that segment will set the current language and that will be considered the root url.
One difference from your requirements is instead of a redirection the default language will be accepted without the language in the url.
Edit: A filter which may help you with the redirect.
Route::filter('pattern: *', array('name' => 'langredirect', function()
{
$uri = Request::server('request_uri');
$segments = explode('/', $uri);
if ( ! array_get(Config::get('application.languages'), $segments[1]) )
{
return Redirect::to(URL::base() . '/' . Config::get('application.language') . $uri);
}
}));