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

harikumar001's avatar

Laravel 5 Catch NotFoundHttpException

I am upgrading from L4 to L5. I had a global NotFoundHttpException in L4 ( app/global.php) and now I added that in App\Exceptions\Handler.php.

    public function render($request, Exception $e)
    {
        if ($e instanceof NotFoundHttpException)
            {
                 return Response::make(['error'=>'not_found','error_message'=>'Please check the URL you submitted'], 404);
            }
        return parent::render($request, $e);
    }
        

But I am not able to catch this error. It always shows the 404 page. What am I missing ?

0 likes
2 replies
harikumar001's avatar

oh. Specifying the full path fixed it.

if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
{
    return Response::make(['error'=>'not_found','error_message'=>'Please check the URL you submitted'], 404);
}
1 like
bobbybouwmann's avatar

It also works if you import the class ;)

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

if ($e instanceof NotFoundHttpException)
{
    // Your stuff here
}
1 like

Please or to participate in this conversation.