What's the real difference between Laravel session () and request () & # 8594; session ()?

I'm working on a Laravel (5.2) project that relies heavily on session, albeit quite new, but I was just curious what the difference is between global session()

and Http request()->session()

, besides the fact that they have different session accessors and writers ?

Here is some information I have about this, from the laravel 5.4 doc,

enter image description here

Unfortunately it doesn't quite help me understand the difference. I also googled and stack overflow, perhaps I could find an answer to no avail. An example is the difference between sessions: :: flash and request- & gt; session-> flash , but I don't really like the answer

What is their real difference in managing session data? I don't mind linking to the documentation where this is located, or even if I have to dig into laravel core.

thank

+10


source to share


3 answers


session () is a helper that gives you faster access to request () -> session ()

Note that request () is also a helper that gives you faster access to the request object.



No difference, it's just a shortcut.

+11


source


I think this will help you: $ request-> session () and session () are the same.

There are two main ways to work with session data in Laravel: the global function in the session () helper and through the $ request instance.



you can use it like this:

public function testMyMethod(Request $request){

    //$userExist = $request->session()->exists('user_id');
    $userExist = $request->session()->has('user_id');

}

      

0


source


Unfortunately a better answer has already been given by Laravel's note; and I can only confirm this because I once noticed such a situation.

I couldn't figure out why the global session('key')

refuses to reflect the value $request->session()->put('key', 'value')

in the same method. I hope I run into the situation again just for proof, but the last answer I would like to give you is:

"There is no difference, it's just a shortcut."

Because, as in the documents mentioned, the difference is noticeable in practice.

0


source







All Articles