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

Marins's avatar

Good practice?

Hello,

I do not know whether this is a good practice. I am trying to redirect the user to the login page after token mismatch.


public function render($request, Exception $exception)
    {
      
     

        if ($e instanceof TokenMismatchException ) {
        return redirect()->route('login');
    }

    return parent::render($request, $e);

But, it is still not working. I get a blank page. How do I redirect to login from here and is it Ok?

Thank you

0 likes
4 replies
36864's avatar

Did you forget to use \Illuminate\Session\TokenMismatchException;? Alternatively,

if($e instanceof  \Illuminate\Session\TokenMismatchException)
Marins's avatar

Thank you I just added it, but still getting the blank page.

36864's avatar
36864
Best Answer
Level 13

Can you add a dd() to check if you're hitting the condition?

public function render($request, Exception $exception)
{
    if ($e instanceof TokenMismatchException ) {
    dd($e);
        return redirect()->route('login');
    }

    return parent::render($request, $e);
}
Marins's avatar

Sorry, I had copy and paste error

Please or to participate in this conversation.