I'm developing a web application using the Laravel framework. I'm using the Laravel session system in a section of my application and have configured it to store sessions in the database.
My problem is that the session set in one of the routes of the application is not displayed correctly in other pages and instead, the value null is returned. I should mention that this problem only occurs when I send a request to the server via AJAX and update the session.
I've checked all the session configuration settings as well as the php.ini and .env settings, but the problem still persists. Do you have any ideas for solving this problem?
https://gist.github.com/realrezamirzaei/3fd51d57a95f90bacd3cc4aec06ba923
"I tried configuring the Laravel session to store sessions in the database, and I have checked the session settings in the php.ini and .env files. However, the session set in one of the routes of my application is not displayed correctly in other pages, and instead, the value null is returned. I was expecting the session to be displayed correctly on all pages."
Hello @realrezamirzaei
Are your api routes defined in the api route group/file?
From: https://laravel.com/docs/10.x/routing#the-default-route-files
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community