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

uksarkar's avatar

Why I am not able to get session() on AppServiceProvider ?

I am trying to get a session data on AppServiceProvider.php. But \Session::get('key') retuning null even \Session::all(); returning []. How can I get session data in AppServiceProvider?

0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@uksarkar

There is a StartSession.php middleware class which is after the service providers being booted. So you should create your own custom middleware in order to access the session, if you need it before any controller action.

https://laravel.com/docs/master/middleware#defining-middleware

Remember that even in a constructor of the controller the session won't work. You will have to wrap it in a middleware. But it does work in any action/method of the controller.

1 like

Please or to participate in this conversation.