如何在Zend视图Helper中访问url变量的值?

时间:2010-04-15 13:27:52

标签: php zend-framework view-helpers

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

我能够获得我的控制器名称:Zend_Controller_Front :: getInstance() - > getRequest() - > getControllerName(); ,但我不知道变量...

提前致谢!

2 个答案:

答案 0 :(得分: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;
   }
}

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

答案 1 :(得分: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)