laravel - 无法在控制器构造函数中获取会话 [英] laravel - Can't get session in controller constructor
本文介绍了laravel - 无法在控制器构造函数中获取会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在新的 Laravel 中,我无法在构造函数中获取会话.为什么?
In new laravel I can't get session in constructor. Why?
public function __construct()
{
dd(Session::all()); //this is empty array
}
然后在下面
public function index()
{
dd(Session::all()); //here works
}
在旧的 Laravel 中,我记得没有这个问题.有什么变化吗?
In old laravel i remember there was not this problem. something changed?
推荐答案
在 Laravel 5.3 中你不能默认这样做.但是,当您编辑 Kernel.php
并将 protected $middleware = [];
更改为以下内容时,它将起作用.
You can't do it by default with Laravel 5.3. But when you edit you Kernel.php
and change protected $middleware = [];
to the following it wil work.
protected $middleware = [
IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,
IlluminateSessionMiddlewareStartSession::class,
IlluminateViewMiddlewareShareErrorsFromSession::class,
];
protected $middlewareGroups = [
'web' => [
AppHttpMiddlewareEncryptCookies::class,
IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
AppHttpMiddlewareVerifyCsrfToken::class,
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
希望这有效!
这篇关于laravel - 无法在控制器构造函数中获取会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文