Support the ongoing development of →
Laravel API Session
Last updated 1 year ago.

Hello @realrezamirzaei

Are your api routes defined in the api route group/file?


All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider. The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group.

That means that if your api route is defined in the api namespace it will by default not have a session so it will not share the session data with your other routes.


I don't use api for this and I work in the same way as the web, but I gave it the middleware api for the controller, but the problem still persists and the appearance of this problem occurs when I want to create a session with a request from the outside of the server.


Can you show the order of requests that you make? So when did you set it and when do you want to access the data? I also see that you access the auth and the cart variables from the session.


I understood the problem When I wanted to request from another domain to the main site, I had a problem. But I realized that this problem can be solved by using the fruitcake/laravel-cors package, and considering that I am using Laravel version 10, there was no need to use this package, it was enough to add it to the kernel file in the web and api section. Add this: \Illuminate\Http\Middleware\HandleCors::class

tvbeek liked this reply

Solution selected by @realrezamirzaei

Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.