-
-
Save dhayab/6172253 to your computer and use it in GitHub Desktop.
| <?php | |
| App::before(function($request)) | |
| { | |
| if ( in_array(Request::segment(1), Config::get('app.languages')) ) { | |
| Session::put('locale', Request::segment(1)); | |
| } else { | |
| return Redirect::to(Config::get('app.locale')); | |
| } | |
| if ( Session::has('locale') ) { | |
| App::setLocale(Session::get('locale')); | |
| } | |
| }); |
| <?php | |
| Route::group(array('prefix' => '{locale}') | |
| Route::get('home', function ( ) | |
| { | |
| return View::make('home'); | |
| }); | |
| )->where(array('locale' => '[a-z]{2}')); |
Anyway, the
->where(array('locale' => '[a-z]{2}'));In routes.php does not seem to work on a Route::group. Too bad you must apply it on each route :s
This works perfect for me....and includes lang code in url....
$languages = array('en', 'ar');
$locale = Request::segment(1);
if (in_array($locale, $languages)) {
\App::setLocale($locale);
} else {
$locale = null;
}
Route::group(array('prefix' => $locale), function()
{
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@getIndex'));
});
my default language is English so I dont include en in url....but when Arabic ar in the url...
That's awesome.
Sorry for the poor formulation of my text.
My English is not very good.
I just did a small modification.
If there is a value on session "locale" variable, and the locale was erased from url, It sets the url for the locale session
filters.php
App::before(function($request)
{
if ( in_array(Request::segment(1), Config::get('app.languages')) ) {
Session::put('locale', Request::segment(1));
} elseif( Session::has('locale') ) { // -------------- HERE!
return Redirect::to(Session::get('locale'));
}else {
return Redirect::to(Config::get('app.locale'));
}
if ( Session::has('locale') ) {
App::setLocale(Session::get('locale'));
}
});routes.php
Route::group(array('prefix' => '{locale}'), function()
{
Route::get('/', array('as' => 'home', function()
{
return View::make('home');
}));
});Hi,
any idea of why I am getting the error in filter.php:
in_array() expects parameter 2 to be array, null given
if ( in_array(Request::segment(1), Config::get('app.languages')) ) {
I am trying to access the site like this localhost/es/finder but even /localhost/finder I get the error.
Thanks
@kuroski thank you.
@notforever to answer your old question, add something like this to app/config.php:
'languages' => Array('en', 'es', 'it', 'de', 'jp')
Hello dhayab,
Nice work here !
Have you any idea on how to "update" the URL helper to make it write the language prefix each time he is used ?
Regards !