Skip to content

Instantly share code, notes, and snippets.

@dhayab
Last active January 25, 2016 16:52
Show Gist options
  • Save dhayab/6172253 to your computer and use it in GitHub Desktop.
Save dhayab/6172253 to your computer and use it in GitHub Desktop.
Laravel persistent language prefixing
<?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}'));
@notforever
Copy link

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

@alana314
Copy link

alana314 commented Nov 5, 2014

@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