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

abusalameh's avatar

How to access session content at boot

Hello there :) I'm trying to access session data from the AppServiceProvider boot method .. but i get null !

any solution ??

Thanks :)

0 likes
7 replies
bashy's avatar

Show some code as to how you're referencing it?

abusalameh's avatar

I've a variable ... that needs its content to be loaded from the session to share it with all views .. any Idea ? @bashy

thomaskim's avatar

You can't by default.

I remember in a github thread where Taylor said that people should be using middleware to handle things like this so create a middleware and then access the session data there.

If you really want to access session data inside your service providers, then you would have to move the web middlewares into the global middleware stack and then do it.

abusalameh's avatar

@thomaskim Can you help me how to create such middleware

I have a shopping cart class I need to get the content of this cart which are stored in the session

I can get all contents using

Cart::content();

I will be very thankful if you explain this thing to me :)

thomaskim's avatar
Level 41

@qahmad Couldn't you do that in the controller or is there a reason you need to do this before you hit the controller?

abusalameh's avatar

Hhhh I was stupid ... How did I forget to share it via the master controller

Thank you @thomaskim It just worked fine

I put the logic in the parent constructure and shared it using view()->share()

And it just worked fine :)

pmall's avatar

I've a variable ... that needs its content to be loaded from the session to share it with all views .. any Idea ?

Session are not available in the service providers as it gets started by a middleware.

However you can setup a view composer sending this session variable to the view. As the view composer callback is fired at the moment the view is rendered, session will be available.

Please or to participate in this conversation.