views:

175

answers:

2

I want to access the value of a specific variable (in the url) in my view helper. How can I do this?

I'm able to get my controller name with: Zend_Controller_Front::getInstance()->getRequest()->getControllerName(); , but I have no idea for the variable ...

Thanks in advance!

+1  A: 

You can get the request object from 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;
   }
}

Now you can use the getRequestVariable-method from all view helpers extending App_View_Helper_Abstract

PatrikAkerstrand
Thanks for the quick respons, but I'm going for the other response.
koko
Sure. My code does the exact same thing, just giving all your view helpers a method for retrieving a parameter without duplicating code.
PatrikAkerstrand
A: 

The most obvious ones are:

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

Also have a look at the API docs:
General
Zend_Controller_Request_Http (1.10)

fireeyedboy
Thanks for the quick respons! Just what I need :-)
koko