Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mpetkov's avatar

L5: Session store not set on request

I recently updated to Laravel 5 and have noticed the following error in my logs. So I am using couchbase as my session driver and all of the settings are exactly the same as I had them in Laravel 4. Everything works and I am not really sure when this error happens and what it prevents from working. Anyone having similar issues?

[2015-06-10 18:11:12] production.ERROR: exception 'RuntimeException' with message 'Session store not set on request.' in /laravel/vendor/compiled.php:2399
Stack trace:
#0 /laravel/vendor/compiled.php(12211): Illuminate\Http\Request->session()
#1 /laravel/vendor/compiled.php(9059): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#2 /laravel/vendor/compiled.php(10917): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#3 /laravel/vendor/compiled.php(9059): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 /laravel/vendor/compiled.php(11922): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#5 /laravel/vendor/compiled.php(9059): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#6 /laravel/vendor/compiled.php(11871): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#7 /laravel/vendor/compiled.php(9059): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#8 /laravel/vendor/compiled.php(2532): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 /laravel/vendor/compiled.php(9059): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 /laravel/vendor/compiled.php(9050): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#12 /laravel/vendor/compiled.php(1945): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#13 /laravel/vendor/compiled.php(1932): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#14 /laravel/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#15 {main}
0 likes
9 replies
automaticAllDramatic's avatar

You are not setting the MiddleWare 'Illuminate\Session\Middleware\StartSession', this could be a cause for this error.

sepehr's avatar

Just a note; In 5.2 web routes should be added under web group, so that the session, csrf and other web-related middlewares get applied to them. For example, if you add auth routes outside of the web route group, you'll get the same exact error.

Let's have a quick look at Illuminate/Foundation/Http/Kernel:

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];
ianspangler's avatar

I am not sure if my issue is directly related to this or not. I am using Laravel 5.2 and when I call csrf_token() to get a token for a form, it returns an empty value. I dug deeper and found that a session is not being registered in SessionServiceProvider. Is there something that needs to be enabled for this to work by default? Since I am a Laravel beginner, I am not sure how to follow the advice above. How do I make sure that my routes are added under the "web" group?

ianspangler's avatar

Okay, so I figured out that routes need to be added within the "web" middleware group like this:

Route::group(['middleware' => 'web'], function () {
Route::get('/my-form', 'MyController@xxxx');
});
3 likes
gildniy's avatar

As @sepehr mensionned, all routes should be included in ['middleware' => 'web'] route group in L5.2.x

phillipdavies's avatar

I think there is actually an issue with L5.2 as any custom error pages display this error if you try to access a session based value (e.g a login form etc).

I assume this is because the exception does not pass through the web middleware group?

To resolve and prevent the 'Session store not set on request' appearing on my custom http error pages I moved:

\Illuminate\Session\Middleware\StartSession::class,

from the web group to the global middleware group in the kernel.php file which solved the issue.

So my kernel.php file now looks like this:


namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
   /**
    * The application's global HTTP middleware stack.
    *
    * These middleware are run during every request to your application.
    *
    * @var array
    */
   protected $middleware = [
       \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
       \Illuminate\Session\Middleware\StartSession::class,
   ];

   /**
    * The application's route middleware groups.
    *
    * @var array
    */
   protected $middlewareGroups = [

       'web' => [
           \App\Http\Middleware\EncryptCookies::class,
           \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
           \Illuminate\View\Middleware\ShareErrorsFromSession::class,
           \App\Http\Middleware\VerifyCsrfToken::class,
       ],

       'api' => [
           'throttle:60,1',
       ],
   ];

   /**
    * The application's route middleware.
    *
    * These middleware may be assigned to groups or used individually.
    *
    * @var array
    */
   protected $routeMiddleware = [
       'auth' => \App\Http\Middleware\Authenticate::class,
       'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
       'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
       'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
   ];
}

If there are any better solutions I'd be glad to hear them as my grip on how to implement middleware is a little tenuous!

5 likes
tptompkins's avatar

Thank you for posting that solution @phillipdavies. I was running into the same issue and I wasn't able to resolve it until I moved \Illuminate\Session\Middleware\StartSession::class into the global middleware. But now I'm curious if this is the "correct" solution and if it will have any potential negative performance impacts. It's really weird how I can't get a session to work with the web middleware. Can anybody shed some light on this?

st_titan's avatar

there are two ways to solve this problem. The first is:move \Illuminate\Session\Middleware\StartSession::class,\Illuminate\View\Middleware\ShareErrorsFromSession::class, from the web group to the global middleware group in the kernel.php file 。 but it's just a "fix", not a solution.

The second is: keep the kernel.php file unchanged。open the routes.php, then add your own route definition into the “Application Routes” for example: before edit. Route::group(['middleware' => ['web']], function () { // });

after edit. Route::group(['middleware' => ['web']], function () { // Route::get('reg', 'login\regController@index'); Route::post('reg/register', 'login\regController@register'); });

Please or to participate in this conversation.