简体   繁体   English

会议如何在Laravel 5中工作

[英]How do sessions work in Laravel 5

I am trying to understand how sessions work in Laravel 5(.4). 我试图了解会话如何在Laravel 5(.4)中工作。 In one hand there are two ways of using them as described in the official documentation : 一方面,有两种使用方式,如官方文档中所述

There are two primary ways of working with session data in Laravel: the global session helper and via a Request instance. 在Laravel中使用会话数据的主要方法有两种:全局会话帮助程序和通过Request实例。

$request->session()->put('key', 'value');

and

session(['key' => 'value']);

The documentation says: 该文件说:

There is little practical difference between using the session via an HTTP request instance versus using the global session helper. 通过HTTP请求实例使用会话与使用全局会话帮助器之间几乎没有实际区别。

But it is never explained what the difference is. 但从未解释有什么区别。

In the other hand there is the "Facade way": 另一方面,存在“外观方式”:

Session::put('key', 'value');

And recently I found this Stack Overflow question How to use session in laravel 5.2 controller . 最近我发现了这个Stack Overflow问题, 如何在laravel 5.2控制器中使用会话 train_fox pointed out this way: train_fox这样指出:

session()->put('key', 'value');

So that makes a total of four ways. 因此共有四种方式。 And I cannot figure out why or when use one or another. 而且我不知道为什么或何时使用一种或另一种。 Does someone know the difference between those four? 有人知道这四个之间的区别吗?

By the way, the only way I could get sessions to work with Redis was with the two last ways. 顺便说一句,我可以使会话与Redis一起工作的唯一方法是使用最后两种方法。

Thank you in advance for your enlightenment. 预先感谢您的启发。

Let's consider Facade first: 让我们首先考虑一下Facade:

Session::put('key', 'value');

This facades calls Illuminate\\Session\\Store::put() . 该外观调用Illuminate\\Session\\Store::put()

Now let's consider the function session() : 现在让我们考虑一下函数session()

function session($key = null, $default = null)
{
    if (is_null($key)) {
        return app('session');
    }

    if (is_array($key)) {
        return app('session')->put($key);
    }
    // ...
}

Reading this, we can assume that session(['a' => 'b']) works similar that session()->put('a', 'b') (because if it's an array, it calls put on the same function). 阅读此内容,我们可以假设session(['a' => 'b'])工作原理类似于session()->put('a', 'b') (因为如果是数组,它将调用put相同的功能)。

app('session') returns Illuminate\\Session\\SessionManager ( https://laravel.com/docs/5.4/facades#facade-class-reference ). app('session')返回Illuminate\\Session\\SessionManagerhttps://laravel.com/docs/5.4/facades#facade-class-reference )。 Illuminate\\Session\\SessionManager has a __call function which in short calls the driver of the session. Illuminate\\Session\\SessionManager具有__call函数,简而言之就是调用会话的驱动程序。 So it's the same behavior. 因此,这是相同的行为。

Now the difference may be in the $request function vs all other ones (as it's written in docs). 现在,区别可能在于$request函数与所有其他函数(如在文档中所写)。 According to the source code it returns a \\Symfony\\Component\\HttpFoundation\\Session\\SessionInterface . 根据源代码,它返回\\Symfony\\Component\\HttpFoundation\\Session\\SessionInterface The SessionInterface has not the same methods as Illuminate\\Session\\Store so maybe it's why it differs. SessionInterfaceIlluminate\\Session\\Store具有不同的方法,因此也许就是原因所在。

Ok I give up. 好吧,我放弃。 It's hard to understand. 很难理解。 I can't help you more, I'm lost. 我无能为力,我迷路了。 I keep this post for history need. 我保留此职位是出于历史需要。

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

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