【问题标题】:Exception Not Rendering View in Laravel异常未在 Laravel 中呈现视图
【发布时间】:2013-01-04 04:30:29
【问题描述】:

问题摘要

我无法让 Laravel 使用 Response::error('500') 呈现 500 个默认错误页面 (views/error/500.php)。

要重现的代码(本示例已简化)

创建一个虚拟控制器并添加以下公共方法:

public function action_index()
{
    try
    {
        throw new Exception;    
    }
    catch(Exception $e)
    {
        Response::error('500');
    }
}

当你运行这个路由的时候,注意你会收到一个空白的白页,并且 header 状态是 200,而不是 500。

预期结果

我希望在views/error/500.php 中呈现默认的500 错误页面,并停止执行其余脚本(假设脚本较长)。

实际结果

标头状态为 200 的空白白页被发送到浏览器。

我的环境

  • Laravel 3.2.12
  • 共享主机上的 PHP 5.3.13
  • Windows 7
  • Chrome 23(在其他各种浏览器上的结果相同)

更多信息

我还有什么可以让这个问题更容易理解或解决的吗? Laravel 论坛 (http://forums.laravel.io/viewtopic.php?id=2191) 上出现了一个类似的(相同的?)问题,并且不久前被声明为已修复。

在 application/routes.php 中,我有以下 500 错误处理程序代码:

Event::listen('500', function()
{
    return Response::error('500');
});

您可以在 GitHub 上的 https://github.com/laravel/laravel/blob/master/laravel/response.php 上查看 Response 对象的源代码。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你错过了“回报”。您必须始终“返回”。

    public function action_index()
    {
        try
        {
            throw new Exception;    
        }
        catch(Exception $e)
        {
            return Response::error('500');
        }
    }
    

    【讨论】:

    • 呃...感谢@TheShiftExchange 的快速正确回答。
    • 没问题 - 我只知道因为我几个小时都遇到同样的问题 - 但至少你再也不会这样做了:)
    【解决方案2】:

    您也可以返回事件,这会立即为您提供相同的效果,但允许您通过更改注册的事件来更改单个位置的行为。

    public function action_index()
    {
        try
        {
            throw new Exception;    
        }
        catch(Exception $e)
        {
            return Event::first('500');
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-28
      • 2019-10-29
      • 1970-01-01
      • 2017-09-14
      • 2012-08-13
      • 1970-01-01
      • 2019-11-12
      • 2018-09-07
      • 2016-12-18
      相关资源
      最近更新 更多