Laravel: Locale Session: Controller gets Parameter to change it but it cant. U have to hardcode it

7.3k views Asked by At

I tried to create a Form and do away with Ajax. Yes, the form at least does send the parameter down to the Controller as I var_dumped it and exited in order to see if it had gotten the new value, and yes it did, but guess what? and here is the question:

Even though the Input::get('locale') received from the Controller is the one I sent from the Form, the following code can't get to change the Session.

Controller:

public function languagechooser()
    {   
         $session = \Input::get('language');
         var_dump($session);exit;
         \Session::set('locale',$session);

         return\Redirect::back();
    }

The only way to change the session is hardcoding it, like this (notice the 'en':

public function languagechooser()
        {   
             $session = \Input::get('language');
             var_dump($session);exit;
             \Session::set('en');

             return\Redirect::back();
        }

but I dont understand why. Once it receives it from the variable, it should stay in there, but it looks that it does not. Is it a variable after all? But on youtube phpacademy does the same thing (just using Laravel 4) while I use 5

and the Form, just a Form

<form action="{!!URL::route('languagechooser')!!}" method  ="post">

<select class="form-control" name="language">
<option value="fr">fr</option>
<option value=en">en</option>
<option value="es">es</option>  
<option value="ru">ru</option>
<option value="it">it</option>
<option value="de">de</option>
</select>   
<button class="btn btn-primary pull-right" type="submit">Search</button>
    {!!Form::token()!!}
</form>
1

There are 1 answers

19
haakym On BEST ANSWER

routes.php

Route::get('/', 'WelcomeController@index');

Route::post('languagechooser', [
    'as' => 'languagechooser',
    'uses' => 'WelcomeController@changeLanguage'
]);

view - welcome.blade.php

<!-- I think this bit should help you out! -->
<p>
    @if( Session::has('locale') )
        Locale: {{ Session::get('locale') }} <br>
        Message: {{ Lang::get('test.message') }}
    @else
        no session locale set
    @endif
</p>


<form action="{!! route('languagechooser') !!}" method = "post">

<select class="form-control" name="language">
    <option value="en">en</option>
    <option value="es">es</option>  
</select>

<button class="btn btn-primary pull-right" type="submit">Search</button>
    {!!Form::token()!!}
</form>

Controller - WelcomeController.php

public function changeLanguage()
{
    $lang = \Input::get('language');

    \Session::put('locale', $lang);

    return \Redirect::back();
}

Create middleware: php artisan make:middleware Locale

Middleware Locale.php

<?php namespace App\Http\Middleware;

use Closure;
use Session;
use App;
use Config;

class Locale {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $language = Session::get('locale', Config::get('app.locale'));

        App::setLocale($language);

        return $next($request);
    }

}

Added this: 'App\Http\Middleware\Locale' to $middleware array in the Http\Kernel.php file so it's loaded on each request.

protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
        'App\Http\Middleware\Locale',
    ];

resources/lang/en/test.php

return [
    'message' => 'hello'
];

resources/lang/es/test.ph`

return [
    'message' => 'hola'
];

Credit to this link: https://laracasts.com/discuss/channels/general-discussion/where-to-setlocale-in-laravel-5-on-multilingual-multidomain-app