简体   繁体   English

Laravel 5 - 会话不起作用

[英]Laravel 5 - session doesn't work

Here's config/session.php :这是config/session.php

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

My storage/framework/sessions have 755 permissions.我的storage/framework/sessions有 755 个权限。

When I put these 2 line in my controller当我将这两行放在我的控制器中时

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

I receive expected "bb" output.我收到预期的"bb"输出。 But if I comment first line:但如果我评论第一行:

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

and refresh page, I still expecting "bb" but getting null .并刷新页面,我仍然期待"bb"但得到null

Also, storage/framework/sessions is empty.此外, storage/framework/sessions是空的。

What should I do to make Session working?我应该怎么做才能使 Session 工作?

Laravel 5 handles sessions via a middleware class called StartSession . Laravel 5 通过名为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:更重要的是,这个中间件是一个TerminableMiddleware并且实际保存数据的代码(在你的情况下保存到会话文件)位于terminate方法中,该方法在请求生命周期结束时运行:

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

When calling dd(Session::get('aa'));当调用dd(Session::get('aa')); the request is being interrupted before the terminate method of the middleware can be called.在调用中间件的terminate方法之前,请求被中断。

Funnily enough, the Laravel Middleware Documentation actually explains Terminable Middleware logic by giving the Laravel StartSession middleware as an example:有趣的是, Laravel 中间件文档实际上以 Laravel StartSession中间件为例解释了可终止中间件的逻辑:

For example, the "session" middleware included with Laravel writes the session data to storage after the response has been sent to the browser.例如,Laravel 中包含的“会话”中间件在响应发送到浏览器后将会话数据写入存储。

That being said, try using var_dump() instead of using dd() .话虽如此,请尝试使用var_dump()而不是dd()

With laravel 5.*, you must change the kernel file like bellow:使用 Laravel 5.*,您必须像下面这样更改内核文件:

  'api' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Session\Middleware\StartSession::class,
            'throttle:60,1',
            'bindings',
        ],

then go to storage/framework/session folder and change the permission to 755 if it has another amount, then delete all files in your storage/framework/session path, use your code again to put something in a session, watch the storage/framework/session folder.然后转到 storage/framework/session 文件夹并将权限更改为 755(如果还有其他数量),然后删除 storage/framework/session 路径中的所有文件,再次使用您的代码将某些内容放入会话中,观看 storage/framework /会话文件夹。

If your session work you can see the weird long file that belong to session right now, and you are done!如果您的会话工作正常,您现在可以看到属于会话的奇怪的长文件,您就完成了!

If your problem is not yet solved, go to config/session and change:如果您的问题尚未解决,请转到 config/session 并更改:

'driver' => env('SESSION_DRIVER', 'file')

to another predefined amount like:到另一个预定义的金额,如:

'driver' => env('SESSION_DRIVER', 'array'),

or even甚至

'driver' => env('SESSION_DRIVER', 'database'),

and finally if you have an empty folder of storage/framework/session, you still have a problem for sure !!!最后,如果你有一个空的 storage/framework/session 文件夹,你肯定还是有问题!!!

在此处输入图片说明

如果您使用 api route ,则您的会话可能会遇到此问题,并且大多数情况下会话都返回 null ,请尝试为此使用 web 路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM