会议如何在Laravel 5中工作 [英] How do sessions work in Laravel 5

查看:92
本文介绍了会议如何在Laravel 5中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解会话在Laravel 5(.4)中的工作方式. 一方面,有两种使用方法,如官方文档所述:

I am trying to understand how sessions work in Laravel 5(.4). In one hand there are two ways of using them as described in the official documentation:

在Laravel中使用会话数据的主要方法有两种:全局会话帮助程序和通过Request实例.

There are two primary ways of working with session data in Laravel: the global session helper and via a Request instance.

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

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

文档说:

通过HTTP请求实例使用会话与使用全局会话帮助器之间几乎没有实际区别.

There is little practical difference between using the session via an HTTP request instance versus using the global session helper.

但从未解释有什么区别.

But it is never explained what the difference is.

另一方面,存在外观方式":

In the other hand there is the "Facade way":

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

最近我发现了这个堆栈溢出问题如何使用laravel 5.2控制器中的会话. train_fox 指出了这种方式:

And recently I found this Stack Overflow question How to use session in laravel 5.2 controller. train_fox pointed out this way:

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?

顺便说一句,我可以使会话与Redis一起工作的唯一方法是最后两种方法.

By the way, the only way I could get sessions to work with Redis was with the two last ways.

预先感谢您的启迪.

推荐答案

让我们首先考虑Facade:

Let's consider Facade first:

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

此外观称为Illuminate\Session\Store::put().

现在让我们考虑功能session():

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

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

阅读此书,我们可以假定session(['a' => 'b'])的工作方式与session()->put('a', 'b')相似(因为如果是数组,它将在同一函数上调用put).

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).

app('session')返回Illuminate\Session\SessionManager( https://laravel.com /docs/5.4/facades#facade-class-reference ). Illuminate\Session\SessionManager具有__call函数,简而言之,该函数调用会话的驱动程序.因此,这是相同的行为.

app('session') returns Illuminate\Session\SessionManager (https://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. So it's the same behavior.

现在,差异可能在$request函数与所有其他函数之间(如在文档中所写).根据源代码,它返回\Symfony\Component\HttpFoundation\Session\SessionInterface. SessionInterfaceIlluminate\Session\Store的方法不同,所以也许是不同的原因.

Now the difference may be in the $request function vs all other ones (as it's written in docs). According to the source code it returns a \Symfony\Component\HttpFoundation\Session\SessionInterface. The SessionInterface has not the same methods as Illuminate\Session\Storeso maybe it's why it differs.

好吧,我放弃.很难理解.我无能为力,我迷路了.我保留此帖子是出于历史需要.

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.

这篇关于会议如何在Laravel 5中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆