16

I'm new to Lumen and want to create an app with this framework. Now I have the problem that if some user enters a wrong url => http://www.example.com/abuot (wrong) => http://www.example.com/about (right), I want to present a custom error page and it would be ideal happen within the middleware level.

Furthermore, I am able to check if the current url is valid or not, but I am not sure how can I "make" the view within the middleware, the response()->view() won't work.

Would be awesome if somebody can help.

what
  • 338
  • 1
  • 6
  • 13
  • Possible duplicate of [Creating custom error page in Lumen](http://stackoverflow.com/questions/30233211/creating-custom-error-page-in-lumen) – James Feb 05 '16 at 04:20
  • @maiorano84 that documentation is for Laravel and won't work the same for Lumen. The exception needs to be caught and handled manually. – James Feb 05 '16 at 04:21
  • It is definitely a duplicate, but unfortunately the answer here is better – Jonathan May 17 '16 at 14:27

4 Answers4

34

Seeing as errors are handled in App\Exceptions\Handler, this is the best place to deal with them.

If you are only after a custom 404 error page, then you could do this quite easily:

Add this line up the top of the Handler file:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Alter the render function to look like so:

public function render($request, Exception $e)
{
    if($e instanceof NotFoundHttpException){
        return response(view("errors.404"), 404);
    }
    return parent::render($request, $e);
}

This assumes your custom 404 page is stored in an errors folder within your views, and will return the custom error page along with a 404 status code.

James
  • 15,754
  • 12
  • 73
  • 91
  • thanks, i used it both on production and development environment – yussan Feb 07 '17 at 13:36
  • 1
    The ```instanceof``` conditional did not work for me. Replaced with ```$e->getStatusCode() == 404``` and it worked. – sigmus Apr 09 '18 at 13:07
  • 2
    for those who the if does not work make sure you import the class `use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;` or specify the full path in the if statement `($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)` – Orlando P. Apr 19 '19 at 18:52
1

You may want to add this so that when for example blade blows up the error page hander will not throw a PHP error.

public function render($request, Exception $exception)
 {
   if (method_exists('Exception','getStatusCode')){

     if($exception->getStatusCode() == 404){
       return response(view("errors.404"), 404);
     }

     if($exception->getStatusCode() == 500){
       return response(view("errors.500"), 404);
     }
   }
   return parent::render($request, $exception);
 }
1

I am using Lumen 8.x version and below solution worked for me:

File path: ‎⁨▸ ⁨app⁩ ▸ ⁨Exceptions⁩ ▸ ⁨Handler.php

public function render($request, Throwable $exception)
{
    // start custom code
    if($exception->getStatusCode() == 404){
        return response(view("errors.404"), 404);
    }
    if($exception->getStatusCode() == 500){
        return response(view("errors.500"), 404);
    }
    // end custom code

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

Do not forget to create errors folder at /resources/views/errors and create the below 2 new files in errors folder:

404.blade.php

500.blade.php

and add html tags and messages in those files what you want to add.

Happy to help you. Thanks for asking this question.

Kamlesh
  • 5,233
  • 39
  • 50
  • This looks promising to me but I get in Lumen 8 due to getStatusCode() not being found. Where is this located ? – AdamJones Sep 15 '21 at 00:35
0

I faced the same situation. response(view("errors.404"), 404)did not work for me, so I changed it as follows:

public function render($request, Exception $exception)
{
    if($exception instanceof NotFoundHttpException){
        return response(view('errors.404')->render(), 404);
    }
    return parent::render($request, $exception);
}