Zend Framework: Accessing Request, Response & Router Object from anywhere

In Zend Framework you can access the current request object from anywhere outside the current controller, for example inside your own base classes. To do so you obtain the singleton instance of the Front Controller Object and access the current request object registered with it.

Here is how you do it:

Zend_Controller_Front::getInstance()->getRequest();

Once you have the access to the Request object, you may access all other methods and objects registered to it. For example you can get the current module name as shown below

Zend_Controller_Front::getInstance()->getRequest()->getModuleName();

Above code snippet is especially useful when you are trying to detect the current request object outside the current controller class.

Accessing the instance of the Front Controller as shown above, gives you the way to access even other objects registered with it like the Response object or the Router Obect, which can be inspected or manipulated further. The below code gives reference to the current Response object.

Zend_Controller_Front::getInstance()->getResponse();

To access the Router object you may use the following code:

Zend_Controller_Front::getInstance()->getRouter();