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

tonz18's avatar

Laravel 9 - Session store not set on request on 404 page using view composers

So I got a bug that I just cant seem to solve:

Im using a view composer to store global variable on all views $currency from sessions() for all views. When accessing a error page eg 404 the $this->request->session() is not set and getting

Error: Session store not set on request.

My View Composer.php

public function __construct(
    protected Request $request,
) {}

public function compose(View $view): void
{
    $currency = $this->request->session()->get('currency', config('app.currency') );

app\Providers\ViewServiceProvider.php

public function boot()
{
    Facades\View::composer('*', CurrencyComposer::class);
}

I have tired the soloution found here

protected $middleware = [
    //...
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
 ];

Which does fix my issue but then creates more issues that im then unable to set any sessions it will refresh all session on every page load.

Other solutions im thinking is creating a if statement to check if sessions exist but that also results into the same error.

// Results as Error - Session store not set on request.
if($this->request->session()) 

Other option I was thinking was checking if its a error page then only setting the variable $currency if its not a error page.

0 likes
1 reply
LaryAI's avatar
Level 58

The issue is that the session middleware is not being applied to the error pages. To fix this, you can add the session middleware to the $middleware property of the app/Http/Kernel.php file.

Here's an example of how to do this:

  1. Open the app/Http/Kernel.php file.

  2. Add the following line to the $middleware property:

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

    This will ensure that the session middleware is applied to all requests.

  3. Save the file and try accessing the error page again.

Here's what the updated $middleware property should look like:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Session\Middleware\StartSession::class,
    // ...
];

If you're still having issues, you can try adding the web middleware group to the $middlewareGroups property of the app/Http/Kernel.php file. This will ensure that the session middleware is applied to all requests that go through the web middleware group.

Here's an example of how to do this:

  1. Open the app/Http/Kernel.php file.

  2. Add the following line to the $middlewareGroups property:

    'web' => [
        \Illuminate\Session\Middleware\StartSession::class,
        // ...
    ],
    

    This will ensure that the session middleware is applied to all requests that go through the web middleware group.

  3. Save the file and try accessing the error page again.

Here's what the updated $middlewareGroups property should look like:

protected $middlewareGroups = [
    'web' => [
        \Illuminate\Session\Middleware\StartSession::class,
        // ...
    ],

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

Please or to participate in this conversation.