Framework: When i use api as route method, i can't get the session values.

Created on 6 Dec 2017  路  7Comments  路  Source: laravel/framework

  • Laravel Version: 5.5.*
  • PHP Version: 7.1.12
  • Database Driver & Version: MySQL 5.7

Description:

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.

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',
],

All 7 comments

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:

  • Laravel Slack (https://larachat.co/)
  • Laravel.io Forum (https://laravel.io/forum)
  • Laracasts Forum (https://laracasts.com/discuss)
  • StackOverflow (http://stackoverflow.com/questions/tagged/laravel)

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

digirew picture digirew  路  3Comments

JamborJan picture JamborJan  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

iivanov2 picture iivanov2  路  3Comments