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

takoChen's avatar

How to use session in every page?

I'm set the session value in API Controller, and direct another page in js. When I try to check the session value in middleware, but I can't get the any value. Here's the way that I set and get the session value.

set value in API Controller: session(['user' => $acct['name']]);

get value in middleware: Log::debug ('my session value: ' . json_encode(session()->all()));

// my log output: my session value: {"_token":"ztN5C2VB2P6otGPxKOPM2z4GSvN2nUBxqqIo9idt"}

all process: login page -> api to check account -> redirect to user page (before loading this page, I set the middleware to check user is login or not. this is the part that I can't get session value)

0 likes
4 replies
Snapey's avatar

api are stateless by default, so session is not maintained. You should place your routes in web.php if you want to use sessions.

1 like

Please or to participate in this conversation.