Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between using the session via an HTTP request instance versus using the global session helper in Laravel

I could not find any information related two types of session access methods. $request->session() from HTTP request instance and session() from session helper in Laravel 5.3 . Is there any difference or which one to use when ?

How to send a get request to below controller method when using P.H.P unit

public function testMyMethod(Request $request){
$userExist = $request->session()->exists('user_id');
}
like image 397
User123456 Avatar asked Mar 02 '17 05:03

User123456


People also ask

How to manage session in Laravel?

By default, Laravel uses file based session storage, which means your session information is stored in files on your server (in storage/framework/sessions ). Even if you change the session storage to use cookies, the session information will be encrypted so that the client side cannot read the data.

Where is Laravel session stored?

The session configuration is stored in config/session. php . Be sure to review the well documented options available to you in this file. By default, Laravel is configured to use the file session driver, which will work well for the majority of applications.


2 Answers

The Service Container is the core of the Laravel architecture. All services, components, and dependencies are registered there, and you can ask for any of them whenever you need it.

But Laravel provides more than one way to "ask for it". You have the global helper functions, you have facades, you have the "proper", "purer" injection of the component instance in the method signature. I think a big part of the philosophy of Laravel is a clean, easy, intuitive API. And in this case, it can be left up to your personal preference to define what "clean and easy" is, and whatever matches your style will be, by definition, intuitive to you.

I know there have been heated debates in the PHP community as to which method is "best", facades have been controversial, traditional dependency injection OOP purists say the only right way may be injecting the object with the controller method signature...

In the end, any of this various methods just grab the object from the same service container bag. It makes no difference performance-wise (I bet two function calls of indirection won't hit your performance) or otherwise. So, use whatever suits your style better. I personally do the "proper" injection anyway if I'm in a typical controller, but maybe I use the global helper if it makes for a cleaner, more readable code in its context.

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Session\Session;
use Facades\Illuminate\Contracts\Session\Session as SessionRealTimeFacade;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request as RequestFacade;
use Illuminate\Support\Facades\Session as SessionFacade;
use PHPUnit\Framework\Assert;

class TestController extends Controller
{
    public function sessionTest(Request $request, Session $session)
    {
        $options = [
            $request->session()->get('_token'),
            session()->get('_token'),
            session('_token'),
            $session->get('_token'),
            SessionFacade::get('_token'),
            SessionRealTimeFacade::get('_token'),
            app('session')->get('_token'),
        ];

        array_reduce(
            $options,
            function ($one, $other) {
                Assert::assertEquals($one, $other);
                return $other;
            },
            array_shift($options)
        );

        return 'All tests passed!';
    }    
}

And by the way, as you can see you have more than just 2 options :)

Of course, this doesn't apply to sessions only, the same goes for getting the request data, getting DB connection, and so much more.

(Also, I think you don't have the real-time facades in 5.3 yet)

like image 132
alepeino Avatar answered Sep 28 '22 03:09

alepeino


  1. Laravel Documentation

According to the Laravel Docs - https://laravel.com/docs/5.3/session - there is 'little practical difference between using the session via an HTTP request instance versus using the global session helper' and that both are testable.

->assertSessionHas($key, $value = null);

Session - Laravel Docs

  1. Laravel Up & Running - Matt Stauffer

In his book (page 320-), Matt talks through the different ways available to access session information:

  • Using the Session facade

    session()->get('user_id');

  • Using the session() method on any given Illuminate Request object

    Route::get('dashboard', function(Request $request) { $request->session()->get('user_id'); });

  • Inject an instance of Illuminate\Session\Store

    Route::get('dashboard', function(Illuminate\Session\Store $session) { return $session->get('user_id'); });

  • Using the global session() helper

    $value = session()->get('key); $value = session('key);

His final comment is: If you're new to Laravel and not sure which to use, I'd recommend using the global helper.

So, I wouldn't worry about the differences and just go with what 'feels right' for you. If you have no preference either way, go with Matt Stauffer's recommendation.

like image 33
kerrin Avatar answered Sep 28 '22 03:09

kerrin