Laravel:语言环境会话:控制器获取参数来更改它,但它不能。你必须对其进行硬编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30686718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: Locale Session: Controller gets Parameter to change it but it cant. U have to hardcode it
提问by patricio
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:
我试图创建一个表单并取消 Ajax。是的,表单至少确实将参数发送到控制器,因为我将它转储并退出以查看它是否获得了新值,是的,确实如此,但你猜怎么着?这是问题:
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.
即使从控制器接收到的 Input::get('locale') 是我从表单发送的,下面的代码也无法更改会话。
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':
更改会话的唯一方法是对其进行硬编码,如下所示(注意“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
但我不明白为什么。一旦它从变量中接收到它,它应该留在那里,但看起来它没有。毕竟它是一个变量吗?但是在 youtube 上 phpacademy 做同样的事情(只使用 Laravel 4)而我使用 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>
回答by haakym
routes.php
路由文件
Route::get('/', 'WelcomeController@index');
Route::post('languagechooser', [
'as' => 'languagechooser',
'uses' => 'WelcomeController@changeLanguage'
]);
view - welcome.blade.php
视图 -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
控制器 - WelcomeController.php
public function changeLanguage()
{
$lang = \Input::get('language');
\Session::put('locale', $lang);
return \Redirect::back();
}
Create middleware: php artisan make:middleware Locale
创建中间件: php artisan make:middleware Locale
Middleware Locale.php
中间件 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.
添加了这个:'App\Http\Middleware\Locale'
到文件中的$middleware
数组,Http\Kernel.php
以便在每个请求时加载它。
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
资源/语言/en/test.php
return [
'message' => 'hello'
];
resources/lang/es/test.ph`
资源/语言/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
归功于此链接:https: //laracasts.com/discuss/channels/general-discussion/where-to-setlocale-in-laravel-5-on-multilingual-multidomain-app