Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mtvs_dev's avatar

Laravel 5 session data is not accessible in the app boot process

In Laravel 5, It seems you can't access the session data in a ServiceProvider::boot(), the Session::get() just returns null while the data exists in the session.

In a package ServiceProvider how can I access to the session data?

I think I should hook to an event, is there an event about loading session in laravel 5, if not what event should I use? I want to share a variable in my views.

0 likes
13 replies
bestmomo's avatar

The provider boot method is fired after all providers register so session should work.

bestmomo's avatar
Level 52

I've checked it, you're right. You could add a middleware.

grit's avatar

I have submitted a proposal here https://github.com/laravel/framework/issues/7906

For now my workaround was to create a new StartSession middleware in app.

    protected function startSession(Request $request)
    {
        $session = parent::startSession($request);

        \Event::fire('session.started');

        return $session;
    }

Then in Kernel.php

    protected $middleware =  [
        // Replace
        'Illuminate\Session\Middleware\StartSession',
        // With
        'App\Http\Middleware\StartSession',
    ]

Then put this in the service providers boot method

    $this->app['events']->listen('session.started', function() {
          dd($this->app['session']->all());
    });

Hope that helps someone else out, it was a very frustrating issue for me.

Edit: just seen your comment franzliedke, the booted method doesn't work either from what I can remember.

It looks like you also need to include

    $this->app->singleton('App\Http\Middleware\StartSession');

In the app service provider. I don't understand why though. Can anyone explain that?

devinfd's avatar

Lets keep this issue going. I believe this is a true bug that should be resolved.

adilsaeed31's avatar

Hi guys,

I know it was too late to respond but I face the session issue in AppServiceProvider Class in (Laravel 5.1) and fixed with below code. use Illuminate\Support\Facades\Session;

and then in boot function add this line of code your session will be set through the app.

Session::put('someKey', 'someValue');

Thanks

2 likes
Howardsun's avatar

We can get session value or data of a loginned user at AppServiceProvider. The following we will show how to get, share values of session and loginned user to all views.

Code:


namespace App\Yourapp\Younamespace;

use Illuminate\Support\ServiceProvider;

use Illuminate\Support\Facades\Session;

use Illuminate\Support\View;

use App;

class AppServiceProvider extends ServiceProvider{

      public function boot() {

           $auth = $this->app['auth']; // get Laravel's Auth facades

 

           view()->composer('*', function($view) use (&$auth) {   // this function coding style is php callback and you can pass variables at boot() to this callback function.

           

 

                 $user = $auth->user(); // get the loginned user

 

                 if ( empty(Session::get('sessionDataWithAssignedKeyYouWant', '')) {

                       Session::put('sessionKeyYouWant', 'sessionDataYouWant'); // set session data

                 }

                 $mySessionDataWithAssignedKey = Session::get('sessionKeyYouWant', 'sessionDataYouWant'); // get session data

 

 

                 $view->with(['user'=> $user, 'myVariable' => $mySessionDataWithAssignedKey]); // share these values to all views.

           });

      }

 

      public function register() {

      }

}

You can change the first parameter at view()->composer() from star sign to what you want. For example, user.* means all views under user directory or this way *.checkout means all view directories has named checkout view. But when you change from star sign to what you want view, you should rather to get and set session data in middleware instead of do them in AppServiceProvider.

Enjoy it.

Inpired by these links:

  1. https://laracasts.com/discuss/channels/general-discussion/laravel-5-session-data-is-not-accessible-in-the-app-boot-process (just this link)

  2. https://stackoverflow.com/a/35314367

Reference: my blog article: http://blog.xuite.net/yhunglee/blog/549548212

aomanansala's avatar

I stumbled upon on this issue as well and approached it in another way. For what its worth I used IoC to call the sessions from there and injected it on my partial views. Would love to hear feedback for this.

Please or to participate in this conversation.