Custom 404 page in Lumen

后端 未结 3 1758
执笔经年
执笔经年 2021-02-04 08:13

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://ww

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 08:51

    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.

提交回复
热议问题