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

标签 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', new Zend_Controller_Router_Route(
        '/customName/:dataVariable', array('controller' => 'YourController', 'action' => 'YourAction')
    ));
}

然后,在 Controller 操作中,您将使用刚刚设置的数据变量来访问它。

例如: 对于类别 - 帖子显示

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

然后在类别 Controller 和发布操作中,我将使用 $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;
  }

}

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

关于php - 如何在 Zend Framework 1.x 中重定向错误页面并执行路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13427782/

相关文章:

javascript - 如何使用 Ajax 和 JSON 制作下拉菜单?

php - 为什么我看不到Zend Framework项目的public文件夹?

zend-framework - 禅德 : Redirect to page after log in with a URL from email

angular - 强制调用 ngOnDestroy 和 ngOnInit

asp.net-mvc - 如何在 ASP.NET MVC 中进行 IgnoreRoute 的单元测试

go - GoLang 上的反射错误 - 参数太少

php - 如何使用 php mysql 中的 select 或 view 函数将下一个空值设置为第一个值?

php - 电子商务数据库设计(多级类别)

使用 magento/bin 设置 :di:compile 在 docker 中耗尽 PHP 内存大小

php - Zend 框架中使用的设计模式