laravel session 自动更新疑惑

WBOY
Release: 2016-06-06 20:11:23
Original
1076 people have browsed it

1.在登录的时候存储 session (userSession);

2.登录后编辑登录用户信息,编辑完成后并没有重新更新userSession;

3.但是在中间件里,获取session的时候却可以得到编辑后的信息 (获取方式:session('userSession'));

不明白这里是为什么?还是在前边处理的时候哪里有问题....

求解!

回复内容:

1.在登录的时候存储 session (userSession);

2.登录后编辑登录用户信息,编辑完成后并没有重新更新userSession;

3.但是在中间件里,获取session的时候却可以得到编辑后的信息 (获取方式:session('userSession'));

不明白这里是为什么?还是在前边处理的时候哪里有问题....

求解!

找到一遍文章 里边这样写:

查看调用相关的代码。laravel编译后,在bootstrap/compiled.php中

<code>

        class Middleware implements HttpKernelInterface  
        {  
            ...  
            public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)  
            {  
                $this->checkRequestForArraySessions($request);  
                if ($this->sessionConfigured()) {  
                    $session = $this->startSession($request); // 启动session  
                    $request->setSession($session);  
                }  
                $response = $this->app->handle($request, $type, $catch); // 调用controller的method  
                if ($this->sessionConfigured()) {  
                    $this->closeSession($session);         //关闭session  
                    $this->addCookieToResponse($response, $session);  
                }  
                return $response;  
            }  
            ...  
         
            protected function closeSession(SessionInterface $session)  
            {  
                $session->save();    // 保存session  
                $this->collectGarbage($session);  
            }  
        }  

</code>
Copy after login

可以看见,在调用完controller之后,调用了session->save()的方法,来主动的保存session。这样session才能落地保存起来,如果在controller或者view里面写了exit;,那么session是不会被保存的,除非主动的写Session::save()才能手工的保存起来。(如果不知道函数调用情况,可以在controller中throw new Exception();,然后在/config/app.php的debug更改为debug=>true。可以看到函数的调用关系。)

但是原理还是不明白!!!因为测试的结果 并不是调用完每个controller 都会修改session。

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!