Laravel全球中间件无法获得会话 [英] Laravel global middleware can't get session

查看:76
本文介绍了Laravel全球中间件无法获得会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 protected $middleware = [
     \App\Http\Middleware\Syspoint::class,
]

use Session;
class Syspoint
{
    echo \Session::get('syspoint');
}

我有一个需要在每次页面请求时运行的中间件,中间件包含会话.

I have a middleware required run everytime when page request, the middleware contain session.

我放置在protected $middleware的内部,但是全局中间件无法获取会话.

I place inside of protected $middleware, but global middleware not able to get session.

推荐答案

您正在调用Session,但尚未启动.

You are calling Session but it is not already started.

如果您需要在中间件中使用Session,则必须将其放在键web下的属性protected $middlewareGroups中,并在调用StartSession之后,即:

If you need Session inside your middleware you have to put it in the property protected $middlewareGroups under the key web and after the call to StartSession, i.e.:

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

这篇关于Laravel全球中间件无法获得会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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