Last active
January 25, 2016 16:52
-
-
Save dhayab/6172253 to your computer and use it in GitHub Desktop.
Laravel persistent language prefixing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::group(array('prefix' => '{locale}') | |
Route::get('home', function ( ) | |
{ | |
return View::make('home'); | |
}); | |
)->where(array('locale' => '[a-z]{2}')); |
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')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works perfect for me....and includes lang code in url....
my default language is English so I dont include en in url....but when Arabic ar in the url...