Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

php - Laravel 5 - session doesn't work

Here's config/session.php:

return [
    'driver' => 'file',
    'files' => storage_path().'/framework/sessions',
];

My storage/framework/sessions have 755 permissions.

When I put these 2 line in my controller

Session::set('aa', 'bb');
dd(Session::get('aa'));

I receive expected "bb" output. But if I comment first line:

// Session::set('aa', 'bb');
dd(Session::get('aa'));

and refresh page, I still expecting "bb" but getting null.

Also, storage/framework/sessions is empty.

What should I do to make Session working?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Laravel 5 handles sessions via a middleware class called StartSession. More importantly, this middleware is a TerminableMiddleware and the code that actually saves the data (in your case to the session file) is located in the terminate method, which is run at the end of the request lifecycle:

public function terminate($request, $response)
{
    if ($this->sessionHandled && $this->sessionConfigured() && ! $this->usingCookieSessions())
    {
        $this->manager->driver()->save();
    }
}

When calling dd(Session::get('aa')); the request is being interrupted before the terminate method of the middleware can be called.

Funnily enough, the Laravel Middleware Documentation actually explains Terminable Middleware logic by giving the Laravel StartSession middleware as an example:

For example, the "session" middleware included with Laravel writes the session data to storage after the response has been sent to the browser.

That being said, try using var_dump() instead of using dd().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...