Skip to content

Instantly share code, notes, and snippets.

@sineld
Created July 6, 2013 05:14
Show Gist options
  • Save sineld/5938739 to your computer and use it in GitHub Desktop.
Save sineld/5938739 to your computer and use it in GitHub Desktop.
Localisation gone wrong?
<?php
// http://forums.laravel.io/viewtopic.php?pid=45912#p45912
First of all, thank you for asking this question, looking into it made me understand things a lot better. As it turns out, you can also use parameters for prefixes in route groups. Using a filter, you can then obtain the value of all parameters (so including the prefix of the route group). What you'll want to do is create a filter (best put in filters.php):
[code]
Route::filter('select_lang', function($route)
{
// do check that the lang parameter is valid!
App:setLocale($route->getParameter('lang'));
});
[/code]
Then, put all routes in a group:
[code]
Route::group(['prefix' => '{lang}', 'before' => 'select_lang'], function()
{
Route::get('foo/{bar}', function($lang, $bar)
{
// do stuff
});
// other routes here
});
[/code]
Do note that all routes (or controller methods) need to have an additional argument at the start, to account for the language argument (it doesn't have to be $lang, can also be $l for example). Furthermore, you should take further steps to make sure there is always a valid language setting as the first argument (best done in the filter), and redirect the argument-less homepage to [url=http://www.yoursite.com/defaultlanguage]www.yoursite.com/defaultlanguage[/url].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment