laravel Lumen 中的自定义 404 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35215716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:11:00  来源:igfitidea点击:

Custom 404 page in Lumen

laravellumen

提问by what

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.

我是 Lumen 的新手,想用这个框架创建一个应用程序。现在我遇到的问题是,如果某些用户输入错误的 url => http://www.example.com/aboot(错误)=> http://www.example.com/about(正确),我想呈现自定义错误页面,最好在中间件级别发生。

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.

此外,我能够检查当前 url 是否有效,但我不确定如何在中间件中“制作”视图, response()->view() 将不起作用。

Would be awesome if somebody can help.

如果有人可以提供帮助,那就太棒了。

回答by James

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

看到在 中处理错误App\Exceptions\Handler,这是处理它们的最佳位置。

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

如果您只是在自定义 404 错误页面之后,那么您可以很容易地做到这一点:

Add this line up the top of the Handlerfile:

将此行添加到Handler文件顶部:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Alter the renderfunction to look like so:

render函数更改为如下所示:

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.

这假设您的自定义 404 页面存储在视图中的错误文件夹中,并将返回自定义错误页面以及 404 状态代码。

回答by Mark Gregory

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

您可能想要添加它,以便例如当刀片炸毁时,错误页面处理程序不会抛出 PHP 错误。

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);
 }

回答by Tomotsugu Kaneko

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

我遇到了同样的情况。response(view("errors.404"), 404)对我不起作用,所以我将其更改如下:

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