【问题标题】:How to use custom 404 page on not found controller/action如何在未找到的控制器/操作上使用自定义 404 页面
【发布时间】:2012-09-06 01:04:49
【问题描述】:

我写了一个自定义的404 Page Not Found 页面。 但是我必须把它放在我的 Zend 应用程序结构中的什么地方以及如何显示它?

当找不到控制器时,我收到Invalid controller specified (dsa) 错误。 当找不到操作时,我会收到 Action "wqe" does not exist and was not trapped in __call() 错误。

简而言之,我不希望出现这些错误。

如何在渲染之前检测页面是否未找到?当我检测到它时,如何显示我的自定义错误页面。目前我从上面收到错误,但仍然显示布局。

我需要一些.htaccess 规则吗?或者它可以用一些 Zend 的东西来完成。

【问题讨论】:

标签: zend-framework http-status-code-404


【解决方案1】:

在您的错误控制器中

class ErrorController extends Zend_Controller_Action {

  public function errorAction() {
    $errors = $this->_getParam('error_handler');

    if (!$errors || !$errors instanceof ArrayObject) {
      $this->view->message = 'You have reached the error page';
      return;
    }

    switch ($errors->type) {
      case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
      case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
      case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
        // 404 error -- controller or action not found
        $this->getResponse()->setHttpResponseCode(404);
        $priority = Zend_Log::NOTICE;
        $this->view->error_code = $this->getResponse()->getHttpResponseCode();
        $this->view->message = "Page Not Found";
        $this->renderScript('error/error_404.phtml');
        break;
      default:
        // application error
        print_r($this->getResponse());
        $this->getResponse()->setHttpResponseCode(500);
        $priority = Zend_Log::CRIT;
        $this->view->error_code = $this->getResponse()->getHttpResponseCode();
        $this->view->message = 'Application error';
        if ($log = $this->getLog()) {
        $log->log($this->view->message, $priority, $errors->exception);
        $log->log('Request Parameters', $priority, $errors->request->getParams());
        $this->renderScript('error/error_500.phtml');
        }

    // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
                $this->view->exception = $errors->exception;
        }

        $this->view->request = $errors->request;
        $this->view->error_code = $this->getResponse()->getHttpResponseCode();
        $this->renderScript('error/error_500.phtml');
        break;
    }

    // Log exception, if logger available
    if ($log = $this->getLog()) {
      $log->log($this->view->message, $priority, $errors->exception);
      $log->log('Request Parameters', $priority, $errors->request->getParams());
    }

    // conditionally display exceptions
    if ($this->getInvokeArg('displayExceptions') == true) {
      $this->view->exception = $errors->exception;
    }

    $this->view->request = $errors->request;
  }

  public function getLog() {
    $bootstrap = $this->getInvokeArg('bootstrap');
    if (!$bootstrap->hasResource('Log')) {
      return false;
    }
    $log = $bootstrap->getResource('Log');
    return $log;
  }

}

【讨论】:

  • 只是为了确认,以上@KarmicDice 代码是 Zend 框架推荐/默认方式吗?因为对我来说来自 $this->renderScript('error/error_404.phtml'); 的内容除非我在页面下方放置 die 语句,否则页面不会加载。
  • 我是一个“摇滚明星 ZF 1x”的家伙......自从我接触它已经有大约 1 年了(在 ZF2 推出之后)。我不确定...但是,如果它仅适用于 die 脚本,则意味着您正在渲染其他页面,因为当您执行 $this->renderScript() 时,它已经发送了标题信息。你注意到我有一个break; 吗?而且这个函数也不做任何其他事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 2020-07-31
  • 2015-09-13
  • 2013-08-31
  • 2013-10-04
  • 2019-02-16
  • 2014-10-12
相关资源
最近更新 更多