When i request url is 'api/test' , i can't get the session values. my test code:
Route::get('session/test',function(){
if (session()->has('test'))
{
echo 'session storage , data:' . session('test');
}else{
session('test','this is test');
dd(session('test'));
}
});
### Steps To Reproduce:
the base method session_start() and $_SESSION['test'] are access values.
Hi @Bevis-Liao,
Welcome to Laravel and we are glad to have you as part of the community.
Unfortunately this github area is not a support area for general application issues. This is only for issues/bugs with the framework code itself. The lack of session data in the API is an intended and correct behaviour of the framework.
Can I please suggestion closing your issue ticket here. Instead trying asking your question on one of the many great community support areas that will likely give you a better answer more quickly:
Thanks in advance.
@laurencei Many thanks for your reply.
This is not a bug. Check the $middlewareGroups in your app/Http/Kernel.php and you'll find that the api does not have sessions enabled.
Solved by adding \Illuminate\Session\Middleware\StartSession::class,
In Kernel.php
Before
'api' => [
'throttle:60,1',
'bindings',
],
After
'api' => [
\Illuminate\Session\Middleware\StartSession::class,
'throttle:60,1',
'bindings',
],
This is not a bug. Check the $middlewareGroups in your app/Http/Kernel.php and you'll find that the api does not have sessions enabled.
I know this is an old post, but would there be some negative outcomes form enabling sessions in the API routes?
@adampatterson I enabled them but it still didn't work
Solved by adding
\Illuminate\Session\Middleware\StartSession::class,In Kernel.php
Before
'api' => [
'throttle:60,1',
'bindings',
],After
'api' => [
\Illuminate\Session\Middleware\StartSession::class,
'throttle:60,1',
'bindings',
],
It didn't work for me at first but when i cleared my config it now works.
php artisan config:clear
Most helpful comment
Solved by adding
\Illuminate\Session\Middleware\StartSession::class,In Kernel.php
Before
'api' => [
'throttle:60,1',
'bindings',
],
After
'api' => [
\Illuminate\Session\Middleware\StartSession::class,
'throttle:60,1',
'bindings',
],