How To Access The Request Object From Any Part Of Your Application

Loading

The request object contains the name of the module, controller, action and the request parameters. Sometimes, you might want to access the request object outside the controller or controller plugin.

For example a user on #zftalk just asked
"how can I access request object within form's method?"

The front controller instance is a singleton. This means we can get the instance of the front controller from any part of our application using the static method getInstance().

To grab the front controller instance use:

<?php
$frontController 
Zend_Controller_Front::getInstance();
?>

We can access the request object from the front controller intance:

$request = $frontController->getRequest();

Using the fluent interface you can access the front controller in a single line:

$request = Zend_Controller_Front::getInstance()->getRequest();

If you want to access the front controller in your action just use:

$request = $this->getRequest();

About the author

Sudheer is an entrepreneur and software developer. Get more from Sudheer on Twitter.


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.