Zend Framework: Getting request object in bootstrap

24,059

Solution 1

If you really want to, you may achieve this calling:

public function _initRequest()
{
    $this->bootstrap('frontController');
    $front = $this->getResource('frontController');
    $front->setRequest(new Zend_Controller_Request_Http());

    $request = $front->getRequest();
}

However, this should be avoided, because the most data you need from the Response object will be available after the front controller is dispatched (eg. module, controller or action name).

The other variables stored in the Response object are extracted from global arrays such as $_SERVER, $_POST or $_GET which you may exceptionally read directly in bootstrap.

But most likely, you want to use Response object in front controller plugin:

class Your_Controller_Plugin_PluginName extends Zend_Controller_Plugin_Abstract
{
     public function preDispatch(Zend_Controller_Request_Abstract $request)
     {
         // do anything with the $request here
     }
}

Solution 2

You shouldn't get the request objet, since if you see the dispatch loop, the idea is that the bootstrap are actions prior to execute in a request.

If you need to alter someway of the application use a Controller Plugin to do that.

Share:
24,059
Can Aydoğan
Author by

Can Aydoğan

Updated on July 05, 2022

Comments

  • Can Aydoğan
    Can Aydoğan almost 2 years

    How do I get the request object from inside the bootstrap file?

    I can try this methods but not work.

    $request= new Zend_Controller_Request_Http();
    $request = Zend_Controller_FrontController::getInstance()->getRequest();
    
  • Can Aydoğan
    Can Aydoğan about 14 years
    No work! Return: "Fatal error: Call to a member function getParam()"
  • Doug
    Doug about 14 years
    I think the second line is supposed to be $this->getResource('frontController')
  • jackyalcine
    jackyalcine about 12 years
    Also, wouldn't this be _initFoo() ?
  • Dharmang
    Dharmang almost 9 years
    preDispatch is the proper method to do this.