如何在未找到的控制器/动作上使用自定义404页面 [英] How to use custom 404 page on not found controller/action

查看:135
本文介绍了如何在未找到的控制器/动作上使用自定义404页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个自定义的404 Page Not Found页面. 但是我必须将其放置在Zend Application结构中的什么位置以及如何显示它?

I have wrote a custom 404 Page Not Found page. But where I have to put it in my Zend Application structure and how to show it?

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

When a controller couldn't be found I receive an Invalid controller specified (dsa) error. And when an action couldn't be found I receive an Action "wqe" does not exist and was not trapped in __call() error.

很快,我不要这些错误.

Shortly, I don't want these errors.

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

How can I detect if the page is not found before rendering? And when I detect it, how can I show my custom error page. At the moment I receive the errors from above but the layout is still displayed.

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

Do I need some .htaccess rules? Or it can be done with some Zend stuff.

推荐答案

在您的ErrorController中

In your ErrorController

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

}

这篇关于如何在未找到的控制器/动作上使用自定义404页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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