我在laravel中使用Session,但是当我在控制器中获得Session时,它不起作用。
web.php
Route::get('locale/{locale}',function ($locale){
//Session::put('locale',$locale);
session(['locale' => $locale]);
Session::save();
return redirect()->back();
});
Localization.php
public function handle($request, Closure $next)
{
if(\Session::has('locale')){
\App::setLocale(\Session::get('locale'));
session(['locale' => \Session::get('locale')]);
\Session::save();
}
return $next($request);
}
Kernel.php
protected $middleware = [
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\Localization::class,
],
'api' => [
\App\Http\Middleware\EventLogMiddle::class,
'throttle:600,1',
'bindings',
],
];
MachCtrl.php(扩展控制器)
public function index()
{
$lang = session('locale');
$config = json_encode([
"table" => "Machine_list",
"title" => "machine list",
"rows" => 20,
"column" => [
"Machine_id", "VM_name", "Machine_location","IP_address",
"Network_status", "Current_status"
],
"CUDP" => "0111",
"CreateRoute" => route('machine.create'),
"DeleteRoute" => route('machine.destroy'),
"UpdateRoute" => route('machine.editInfo'),
"locale" => $lang,
]);
return view('table.show')->with(['config' => $config, 'lang' =>
$lang]);
}
我将Session保存在路由器和中间件中,这是行不通的。
但是当我将Session放入Controller时,它就可以工作了。如下代码:
public function index()
{
session(['locale' => 'en']);
$lang = session('locale');
$config = json_encode([
"table" => "Machine_list",
"title" => "machine list",
"rows" => 20,
"column" => [
"Machine_id", "VM_name", "Machine_location","IP_address",
"Network_status", "Current_status"
],
"CUDP" => "0111",
"CreateRoute" => route('machine.create'),
"DeleteRoute" => route('machine.destroy'),
"UpdateRoute" => route('machine.editInfo'),
"locale" => $lang,
]);
return view('table.show')->with(['config' => $config, 'lang' =>
$lang]);
}
我真的很想知道我的项目中发生了什么。 谢谢。
编辑: 我在代码中使用dd(session()):
-web.php
Route::get('locale/{locale}',function ($locale){
Session::put('locale',$locale);
Session::save();
dd(session());
return redirect()->back();
});
结果:
-Localization.php
public function handle($request, Closure $next)
{
if(\Session::has('locale')){
\App::setLocale(\Session::get('locale'));
session(['locale' => \Session::get('locale')]);
//dd(session());
\Session::save();
}
return $next($request);
}
结果:
-MachCtrl.php
public function index()
{
dd(session());
$lang = session('locale');
$config = json_encode([
"table" => "Machine_list",
"title" => "machine list",
"rows" => 20,
"column" => [
"Machine_id", "VM_name", "Machine_location","IP_address",
"Network_status", "Current_status"
],
"CUDP" => "0111",
"CreateRoute" => route('machine.create'),
"DeleteRoute" => route('machine.destroy'),
"UpdateRoute" => route('machine.editInfo'),
"locale" => $lang,
]);
return view('table.show')->with(['config' => $config, 'lang' =>
$lang]);
}
答案 0 :(得分:1)
尝试使用其他会话驱动程序来解决问题。
编辑:对不起,我还不能发表评论,<50rep:(
答案 1 :(得分:1)
我检查了我的工作代码。我将整个周期设置为:
语言中间件:
class Language
{
protected $app;
public function __construct(Application $app, Request $request) {
$this->app = $app;
}
public function handle($request, Closure $next)
{
$this->app->setLocale(session('user_locale', config('app.locale')));
return $next($request);
}
}
在我的中间件Kernel.php中:
protected $middlewareGroups = [
'web' => [
\Illuminate\Session\Middleware\StartSession::class,
//...
\App\Http\Middleware\Language::class,
]
];
并在途中:
Route::get('locale/{locale}',function ($locale){
session(['user_locale' => $locale]);
});