慕莱坞森
哈哈,我之前也是被这个搞蒙了很长一段时间。
不过后来解决了,很简单,不在写入session的地方使用dd,dump等方法。
laravel保存session是在中间件中进行的,源代码如下
public function handle($request, Closure $next)
{
$this->sessionHandled = true;
// If a session driver has been configured, we will need to start the session here
// so that the data is ready for an application. Note that the Laravel sessions
// do not make use of PHP "native" sessions in any way since they are crappy.
if ($this->sessionConfigured()) {
$session = $this->startSession($request);
$request->setSession($session);
}
$response = $next($request);
// Again, if the session has been configured we will need to close out the session
// so that the attributes may be persisted to some storage medium. We will also
// add the session identifier cookie to the application response headers now.
if ($this->sessionConfigured()) {
$this->storeCurrentUrl($request, $session);
$this->collectGarbage($session);
$this->addCookieToResponse($response, $session);
}
return $response;
}
保存session的function
protected function addCookieToResponse(Response $response, SessionInterface $session)
{
if ($this->usingCookieSessions()) {
$this->manager->driver()->save();
}
if ($this->sessionIsPersistent($config = $this->manager->getSessionConfig())) {
$response->headers->setCookie(new Cookie(
$session->getName(), $session->getId(), $this->getCookieExpirationDate(),
$config['path'], $config['domain'], Arr::get($config, 'secure', false)
));
}
}
可以看到,保存session的方法在 $response = $next($request) 之后进行的,你在你的代码中使用了dd()、dump()、exit()等方法,后面的保存session的代码将不能执行。