【问题标题】:Lumen 5.5 NotFoundHttpException handlerLumen 5.5 NotFoundHttpException 处理程序
【发布时间】:2018-04-14 04:09:56
【问题描述】:

我在 Lumen 5.5 和我的错误处理程序中运行一个小应用程序,当我将视图作为我的响应内容传递时,标头收到错误 500 而不是 404。

我添加了一个示例 sn-p,请考虑我只会返回一个或另一个响应!

文件:app/Exceptions/Handler.php

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
 public function render($request, Exception $e) {
    if($e instanceof NotFoundHttpException) {

        // This gives me a 404 in the browser dev console but not in the headers
        return response(view("errors.404"), 404);

        // This gives me a 404 in the headers
        return response('404 error', 404);

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

如果我在浏览器 DevTools 中加载此页面,我的 GET 状态为 404,但如果我使用 http 检查器工具在线扫描,我会收到错误 500。

这弄乱了我的 Adwords 广告系列,所以我不得不换一个简单的回复。

因为它是 Lumen,所以我不能使用在 Laravel 中可以使用的以下内容:

return response(view('error.404', [], 404));

非常感谢您。

【问题讨论】:

  • 视图在几个点之前从 Lumen 中删除。我认为可能有办法通过将代码添加到某些配置文件来添加它们,但似乎不再将视图视为项目的一部分。
  • 好的,谢谢。为了下一个项目,我从 Lumen 搬到了 Laravel,我没有使用好的工具来完成这项工作。

标签: php http lumen


【解决方案1】:

这个怎么样:

use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

if ($e instanceof NotFoundHttpException) {
    return new Response(
        view('errors.404'),
        $e->getStatusCode(),
        $e->getHeaders()
    );
}

【讨论】:

  • 抱歉回复晚了。通过在线的每个 http 状态工具,标题仍然是 500 错误,但是我自己的浏览器给了我 404 错误。我只是放弃了模板渲染,因为它是一个生命周期短的小着陆页。
猜你喜欢
  • 2019-12-04
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 2018-01-04
  • 2021-01-06
  • 2018-02-27
相关资源
最近更新 更多