如何重定向错误页面并在Zend Framework 1.x中执行路由 [英] How to redirect Error Page and perform Routes in Zend Framework 1.x

查看:104
本文介绍了如何重定向错误页面并在Zend Framework 1.x中执行路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to perform routes in Zend Framework 1.x and also redirect custom error messages?

推荐答案

要执行自定义路由:

在您的 bootstrap.php 文件中放入以下内容

Put the following in your bootstrap.php file

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

然后,在控制器操作中,将使用数据变量访问该变量

then, in your controller action, you will access it with a data variable which you have just set.

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

For example: For Category - Post display

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

然后在类别控制器和后期处理中,我将使用 $ this-> getRequest()-> getParam('postId');

then in category controller and post action, I will use $this->getRequest()->getParam('postId');

会将网址的框架设置为:example.com / post / 123

which will frame url as : example.com/post/123

ZF1为您提供了预定义的ErrorController,您可以使用它。

ZF1 comes you a predefined ErrorController, you can use it.

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 ,但是请确保您为用户的浏览器和搜索机器人提供了正确的状态。

You can set HTTP_STATUS_CODES from the list but, make sure you give user's browser and search bots the right statuses.

这篇关于如何重定向错误页面并在Zend Framework 1.x中执行路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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