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

petercontrains's avatar

Routes NotFoundHttpException

How can you handle route errors for curious users who could inject other non-existent routes and generate errors, and not show error screenshot but direct or control this error of non-existent routes?

for example: http://localhost/kdjlkdjgdjgkdjgldjldgjdlgjdk

Symfony\Component\HttpKernel\Exception NotFoundHttpException

in /var/www/html/ws/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php (line 230)

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

If your APP_DEBUG is false in the .env file, then you will not see that particular view; you will get a more generic view instead.

Additionally, you can handle the Symfony\Component\HttpKernel\Exception\NotFoundHttpException exception in app/Exceptions/Handler.php:

public function render($request, Throwable $exception)
{
        if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
            if ($request->wantsJson()) {
                return response()->json(['message' => 'No way José'],  404);
            }

            return redirect('/');
        }
        return parent::render($request, $exception);
 }
petercontrains's avatar

turned out, that was it, thank you very much It remained like this:

public function render($request, Throwable $exception) { if ($exception instanceof NotFoundHttpException) {

                    return response()->json(['message' => 'Not Found'],  404);
              

               
            }
            return parent::render($request, $exception);
     }

Please or to participate in this conversation.