laravel-无法在控制器构造函数中获取会话 [英] laravel - Can't get session in controller constructor

查看:352
本文介绍了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 = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
];

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

希望这行得通!

这篇关于laravel-无法在控制器构造函数中获取会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆