0

In Laravel you can show a 404 page just on creating a file 404.blade.php in the errors folder according to this answer.

But how can I return a custom response in my Lumen API if the entered request don't match any of mine in web.php. So something like:

// ...
$router->default(function () {
    return response()->json(['error' => 'INVALID_ROUTE'], 404);
});
johannchopin
  • 13,720
  • 10
  • 55
  • 101
  • I think there is not a fallback method on lumen, but you can handle it on App\Exceptions\Handler class. Take a look at this https://stackoverflow.com/questions/35215716/custom-404-page-in-lumen – porloscerros Ψ May 11 '20 at 18:06
  • This post is to render a view but I want to return a json response. – johannchopin May 11 '20 at 18:32
  • 2
    You can return a json response like the one in your question. Just change the response in the return `if($exception instanceof NotFoundHttpException){ return response()->json(['error' => 'INVALID_ROUTE'], 404); }` and add this line up the top of the Handler file: `use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;`. I don't have a lumen project to test it but should work. – porloscerros Ψ May 11 '20 at 19:43
  • The Handler file referred to is in App\Exceptions\Handler.php – fokosun May 30 '20 at 05:57

0 Answers0