Zend Framework: Getting request object in bootstrap

社会主义新天地 提交于 2019-12-30 05:57:55

问题


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();

回答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
     }
}



回答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.




回答3:


You need to bootstrap the frontController first, try something like:

function initFoo()
{
    $this->bootstrap('frontController');
    $req = $this->frontController->getRequest();
}


来源:https://stackoverflow.com/questions/2622879/zend-framework-getting-request-object-in-bootstrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!