laravel 在 Lumen 中创建自定义错误页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30233211/
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 11:32:52  来源:igfitidea点击:

Creating custom error page in Lumen

laravelbladelumen

提问by Kusmayadi

How do I create custom view for errors on Lumen? I tried to create resources/views/errors/404.blade.php, like what we can do in Laravel 5, but it doesn't work.

如何为 Lumen 上的错误创建自定义视图?我试图创建resources/views/errors/404.blade.php,就像我们在 Laravel 5 中可以做的那样,但它不起作用。

回答by lukasgeiter

Errors are handled within App\Exceptions\Handler. To display a 404 page change the render()method to this:

错误在App\Exceptions\Handler. 要显示 404 页面,请将render()方法更改为:

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

And add this in the top of the Handler.php file:

并将其添加到 Handler.php 文件的顶部:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Edit:As @YiJiang points out, the response should not only return the 404 view but also contain the correct status code. Therefore view()should be wrapped in a response()call passing in 404as status code. Like in the edited code above.

编辑:正如@YiJiang 指出的那样,响应不仅应返回 404 视图,还应包含正确的状态代码。因此view()应该包装response()404作为状态代码传入的调用中。就像上面编辑过的代码一样。

回答by Yi Jiang

The answer by lukasgeiteris almostcorrect, but the response made with the viewfunction will always carry the 200HTTP status code, which is problematic for crawlers or any user agent that relies on it.

lukasgeiter 的回答几乎是正确的,但是使用该view函数做出的响应将始终携带200HTTP 状态代码,这对于爬虫或任何依赖它的用户代理来说都是有问题的。

The Lumen documentationtries to address this, but the code given does not work because it is copied from Laravel's, and Lumen's stripped down version of the ResponseFactoryclass is missing the viewmethod.

流明文档试图解决这个问题,但因为它是从Laravel的复制,并流明的向下的版本剥离给出的代码不起作用ResponseFactory类缺少view方法。

This is the code which I'm currently using.

这是我目前使用的代码。

use Symfony\Component\HttpKernel\Exception\HttpException;

[...] 

public function render($request, Exception $e)
{
    if ($e instanceof HttpException) {
        $status = $e->getStatusCode();

        if (view()->exists("errors.$status")) {
            return response(view("errors.$status"), $status);
        }
    }

    if (env('APP_DEBUG')) {
        return parent::render($request, $e);
    } else {
        return response(view("errors.500"), 500);
    }
}

This assumes you have your errors stored in the errorsdirectory under your views.

这假设您将错误存储在errors视图下的目录中。

回答by Kennith

It didn't work for me, but I got it working with:

它对我不起作用,但我得到了它:

if($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
  return view('errors.404');
}

You might also want to add

您可能还想添加

http_response_code(404) 

to tell the search engines about the status of the page.

告诉搜索引擎页面的状态。