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

thusk's avatar
Level 3

How to use session inside render method of Exceptions/Handler

Hi there, im having a trouble with use of Laravel Session componnent, my page 404 (error) needs to extend the default template, and the default template uses session values.

I know its possible to listen the event inside this method, but there isnt any way to use session() helper inside 404.blade.php =/

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if($exception instanceof  NotFoundHttpException){
            return response()->view('errors.404', [], 404);
        }
        return parent::render($request, $exception);
    }

Can someone help me to get a instance of session there?.

0 likes
3 replies
DmytroOlefyrenko's avatar

Hi Thusk! You can get session this way:

$request->session()->get('key');

Since you have \Illuminate\Http\Request as a parameter. Or can use session function:

session('your_key');

Hope it will help!

jaggu's avatar

I think using localstorage would be better approach for SPA.

Please or to participate in this conversation.