Laravel:区域设置会话:控制器获取参数来更改它,但它不能。你必须硬编码它
Laravel: Locale Session: Controller gets Parameter to change it but it cant. U have to hardcode it
我试图创建一个表单并取消 Ajax。是的,表单至少确实在我 var_dumped 时将参数发送到控制器并退出以查看它是否已获得新值,是的,它确实获得了新值,但你猜怎么着?这是问题:
即使从Controller收到的Input::get('locale')是我从Form发送的,下面的代码也无法改变Session。
控制器:
public function languagechooser()
{
$session = \Input::get('language');
var_dump($session);exit;
\Session::set('locale',$session);
return\Redirect::back();
}
更改会话的唯一方法是对其进行硬编码,就像这样(注意 'en':
public function languagechooser()
{
$session = \Input::get('language');
var_dump($session);exit;
\Session::set('en');
return\Redirect::back();
}
但我不明白为什么。一旦它从变量接收到它,它应该留在那儿,但看起来它没有。它毕竟是一个变量吗?但是在 youtube 上,phpacademy 做同样的事情(只是使用 Laravel 4),而我使用 5
还有表单,只是一个表单
<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
Route::get('/', 'WelcomeController@index');
Route::post('languagechooser', [
'as' => 'languagechooser',
'uses' => 'WelcomeController@changeLanguage'
]);
查看 - 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>
控制器 - WelcomeController.php
public function changeLanguage()
{
$lang = \Input::get('language');
\Session::put('locale', $lang);
return \Redirect::back();
}
创建中间件:php artisan make:middleware Locale
中间件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);
}
}
将此添加:'App\Http\Middleware\Locale'
到 Http\Kernel.php
文件中的 $middleware
数组,以便在每次请求时加载它。
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'
];
我试图创建一个表单并取消 Ajax。是的,表单至少确实在我 var_dumped 时将参数发送到控制器并退出以查看它是否已获得新值,是的,它确实获得了新值,但你猜怎么着?这是问题:
即使从Controller收到的Input::get('locale')是我从Form发送的,下面的代码也无法改变Session。
控制器:
public function languagechooser()
{
$session = \Input::get('language');
var_dump($session);exit;
\Session::set('locale',$session);
return\Redirect::back();
}
更改会话的唯一方法是对其进行硬编码,就像这样(注意 'en':
public function languagechooser()
{
$session = \Input::get('language');
var_dump($session);exit;
\Session::set('en');
return\Redirect::back();
}
但我不明白为什么。一旦它从变量接收到它,它应该留在那儿,但看起来它没有。它毕竟是一个变量吗?但是在 youtube 上,phpacademy 做同样的事情(只是使用 Laravel 4),而我使用 5
还有表单,只是一个表单
<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
Route::get('/', 'WelcomeController@index');
Route::post('languagechooser', [
'as' => 'languagechooser',
'uses' => 'WelcomeController@changeLanguage'
]);
查看 - 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>
控制器 - WelcomeController.php
public function changeLanguage()
{
$lang = \Input::get('language');
\Session::put('locale', $lang);
return \Redirect::back();
}
创建中间件:php artisan make:middleware Locale
中间件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);
}
}
将此添加:'App\Http\Middleware\Locale'
到 Http\Kernel.php
文件中的 $middleware
数组,以便在每次请求时加载它。
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'
];