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>
routes.php
view - welcome.blade.php
Controller - WelcomeController.php
Create middleware:
php artisan make:middleware Locale
Middleware Locale.php
Added this:
'App\Http\Middleware\Locale'
to$middleware
array in theHttp\Kernel.php
file so it's loaded on each request.resources/lang/en/test.php
resources/lang/es/test.ph`
Credit to this link: https://laracasts.com/discuss/channels/general-discussion/where-to-setlocale-in-laravel-5-on-multilingual-multidomain-app