2

我想在我的视图助手中访问特定变量(在 url 中)的值。我怎样才能做到这一点?

我可以通过以下方式获取我的控制器名称: Zend_Controller_Front::getInstance()->getRequest()->getControllerName(); ,但我不知道变量...

提前致谢!

4

2 回答 2

4

您可以从以下位置获取请求对象Zend_Controller_Front

abstract class App_View_Helper_Abstract extends Zend_View_Helper_Abstract
{
   /**
    * @var Zend_Controller_Front
    */
   private $_frontController;

   /**
    * Convience function for getting a request parameter from the request
    * object in a view helper
    * @param string $name The name of the request parameter
    * @param mixed $default The value to return if $name is not defined in the
    * request
    * @return mixed The value of parameter $name in the request object,
    * or $default if $name is not defined in the request
    */
   public function getRequestVariable ($name, $default = null)
   {
      return $this->getRequest()->getParam($name, $default);
   }

   /**
    *
    * @return Zend_Controller_Request_Abstract
    */
   public function getRequest ()
   {
      return $this->getFrontController()->getRequest();
   }

   /**
    * @return Zend_Controller_Front
    */
   private function getFrontController ()
   {
      if ( empty($this->_frontController) )
      {
         $this->_frontController = Zend_Controller_Front::getInstance();
      }
      return $this->_frontController;
   }
}

现在您可以getRequestVariable从所有扩展 App_View_Helper_Abstract 的视图助手中使用 -method

于 2010-04-15T13:41:05.883 回答
2

最明显的是:

// will retrieve any param set in the request (might even be route param, etc)
Zend_Controller_Front::getInstance()->getRequest()->getParam( 'someParam' );

// $_POST
Zend_Controller_Front::getInstance()->getRequest()->getPost( 'somePostParam' );

// $_GET
Zend_Controller_Front::getInstance()->getRequest()->getQuery( 'someQueryStringParam' );

还可以查看 API 文档:
General
Zend_Controller_Request_Http (1.10)

于 2010-04-15T13:43:09.407 回答