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

mostafa.miri65's avatar

set status text with abort function

  • Laravel Version: 5.5.14
  • PHP Version: 7.1.10

Description:

When I try to POST with X-Requested-With='XMLHttpRequest' without CSRF-TOKEN I receive null message in response. I add this code to App\Exceptions\Handler:

public function render($request, Exception $exception)
    {
        if($exception instanceof TokenMismatchException) {
            abort(419, 'Token Mismatch OR page has expired due to inactivity.');
        }
        return parent::render($request, $exception);
    }

Now I have Token Mismatch OR page has expired due to inactivity. message in response, but status text is: unknown status.

How can I set status text?

0 likes
5 replies
Snapey's avatar

How about

    abort(419, ['Status'=>'Token Mismatch OR page has expired due to inactivity.']);
Snapey's avatar

OK, this works;

    public function render($request, Exception $exception)
    {
        if($exception instanceof \Illuminate\Session\TokenMismatchException) {
            return (new \Illuminate\Http\Response)->setStatusCode(419, 'Token Mismatch OR page has expired due to inactivity.');
        }
        return parent::render($request, $exception);
    }

You need to a) specify the namespace of the exception, and b) return a response object

mostafa.miri65's avatar

It's correct status text, but response can not throw new HttpException.I'm looking for a way that do it with a HttpException

Please or to participate in this conversation.