在流明中创建自定义错误页面 [英] Creating custom error page in Lumen

查看:58
本文介绍了在流明中创建自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为流明上的错误创建自定义视图?我试图创建resources/views/errors/404.blade.php,就像我们在Laravel 5中所能做的那样,但这是行不通的.

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.

推荐答案

lukasgeiter的答案几乎是 是正确的,但是使用view函数做出的响应将始终带有200 HTTP状态代码,这对于爬网程序或依赖它的任何用户代理都是有问题的.

The answer by lukasgeiter is almost correct, but the response made with the view function will always carry the 200 HTTP status code, which is problematic for crawlers or any user agent that relies on it.

流明文档试图解决这个问题,但是给出的代码没有之所以有效,是因为它是从Laravel的副本复制而来的,而Lumen的精简版ResponseFactory类缺少了view方法.

The Lumen documentation tries 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 ResponseFactory class is missing the view method.

这是我当前正在使用的代码.

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

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

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

这篇关于在流明中创建自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆