Как получить доступ к значению переменной URL в Zend View Helper?

Я хочу получить доступ к значению определенной переменной (в URL-адресе) в моем помощнике представления. Как я могу это сделать?

Я могу получить имя моего контроллера с помощью: Zend_Controller_Front::getInstance()->getRequest()->getControllerName(); , но я понятия не имею о переменной...

Заранее спасибо!


person koenHuybrechts    schedule 15.04.2010    source источник


Ответы (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:
Общие
Zend_Controller_Request_Http (1.10)

person Decent Dabbler    schedule 15.04.2010
comment
Спасибо за быстрые ответы! Как раз то, что мне нужно :-) - person koenHuybrechts; 15.04.2010

Вы можете получить объект запроса от 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.

person PatrikAkerstrand    schedule 15.04.2010
comment
Спасибо за быстрые ответы, но я иду за другим ответом. - person koenHuybrechts; 15.04.2010
comment
Конечно. Мой код делает то же самое, просто предоставляя всем вашим помощникам представления метод для получения параметра без дублирования кода. - person PatrikAkerstrand; 15.04.2010