'How to fix `There is currently no session available` after an API request call to Laravel 9

After Laravel project upgrade to PHP8 and laravel 9 on the all API endpoints started getting: There is currently no session available.

  • Upgrade was made from laravel 8 to laravel 9.
  • PHP was upgraded from 7.4 to 8.1

Full backtrace:

{
   "message": "There is currently no session available.",
   "status_code": 500,
   "debug": {
       "line": 515,
       "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Http/Request.php",
       "class": "Symfony\\Component\\HttpFoundation\\Exception\\SessionNotFoundException",
       "trace": [
           "#0 /var/www/html/vendor/dingo/api/src/Http/Request.php(39): Illuminate\\Http\\Request->getSession()",
           "#1 /var/www/html/vendor/dingo/api/src/Http/Middleware/Request.php(99): Dingo\\Api\\Http\\Request->createFromIlluminate(Object(Illuminate\\Http\\Request))",
           "#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
           "#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
           "#4 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(142): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
           "#5 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(111): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))",
           "#6 /var/www/html/public/index.php(52): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))",
           "#7 {main}"
       ]
   }
}

Project is dockerized and Dockerfile's were upgraded to php-8.1-apache. Also dependencies fixed after running composer update

These are the most important composer.json requires:

 "php": "^8.1",
 "dingo/api": "dev-l9-compatibility",
 "laravel/framework": "^9.0",
....

It seems to me that error comes from the dingo api:

Illuminate/HTTP/Request.php line 514:

    public function getSession(): SessionInterface
    {
        return $this->hasSession()
                    ? new SymfonySessionDecorator($this->session())
                    : throw new SessionNotFoundException;
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source