1

I have been working with session in web controllers but never tried in api controllers.I want session works in api controllers.

here is my method in Api/TestController

public function setSession(Request $request){
  //   session()->put('hellos','rfer');
    if(session()->has('hellos')){
        return response()->json(true);
    }
    else{
        return response()->json(false);
    }
}

if I comment session->put().. The method return false.

Then I tried to register in Kernel.php like that

  'api' => [
        'throttle:60,1',
        'bindings',
        \Illuminate\Session\Middleware\StartSession::class,
    ],

But I still don't get what i want. I want to set the session value from api controller for all routes of my project.

Yan Myo Aung
  • 398
  • 1
  • 6
  • 18

1 Answers1

1

They do work. See this thread.

After updating Kernel.php with below code, delete all registered sessions from session driver (whether it's file, database, redis, etc) and browse your app again.

'api' => [
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Session\Middleware\StartSession::class,
    ....
],
user8555937
  • 2,161
  • 1
  • 14
  • 39