Php 如何在Zend Framework 1.x中重定向错误页面和执行路由

Php 如何在Zend Framework 1.x中重定向错误页面和执行路由,php,zend-framework,routes,zend-route,Php,Zend Framework,Routes,Zend Route,如何在Zend Framework 1.x中执行路由并重定向自定义错误消息?要执行自定义路由: 将以下内容放入bootstrap.php文件中 function _initRoutes() { $front_controller = Zend_Controller_Front::getInstance(); $router = $front_controller->getRouter(); $router->addRoute('customName', ne

如何在Zend Framework 1.x中执行路由并重定向自定义错误消息?

要执行自定义路由:

将以下内容放入
bootstrap.php
文件中

function _initRoutes() {
    $front_controller = Zend_Controller_Front::getInstance();
    $router = $front_controller->getRouter();

    $router->addRoute('customName', new Zend_Controller_Router_Route(
        '/customName/:dataVariable', array('controller' => 'YourController', 'action' => 'YourAction')
    ));
}
然后,在控制器操作中,您将使用刚刚设置的数据变量访问它

例如: 用于类别-张贴显示

function _initRoutes() {
    $front_controller = Zend_Controller_Front::getInstance();
    $router = $front_controller->getRouter();

    $router->addRoute('category', new Zend_Controller_Router_Route(
        '/post/:postId', array('controller' => 'category', 'action' => 'post')
    ));
}
然后在category controller和post action中,我将使用
$this->getRequest()->getParam('postId')

它将把url框为:example.com/post/123

ZF1为您提供了一个预定义的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;
  }

}

您可以从列表中进行设置,但请确保为用户的浏览器和搜索机器人提供了正确的状态。

@hakre我只是想为任何学习ZF1的人编写一个“全合一”。。。我一直在用同样的东西帮助人们,所以在阅读后想到了创作:KarmicDice:这是完全正确的,请不要对我的快速评论感到生气。你可能想用你的问题稍微概括一下最初的问题是什么。我可以看出这两个问题都属于对方。我也没有注意到你回答了自己的问题。所以我认为如果你对给出的答案不自信,这可能会对你有所帮助。但很明显,这已经有点过时了:)我被纠正了。是的,我也这样做,回答自己的问题。我认为这是个好主意。