php Zend Framework:在引导程序中获取请求对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2622879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:08:33  来源:igfitidea点击:

Zend Framework: Getting request object in bootstrap

phpzend-frameworkrequestbootstrapping

提问by Can Aydo?an

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

回答by takeshin

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).

但是,应该避免这种情况,因为在调度前端控制器(例如,模块、控制器或操作名称)之后,您需要的来自 Response 对象的大部分数据都将可用。

The other variables stored in the Response object are extracted from global arrays such as $_SERVER, $_POSTor $_GETwhich you may exceptionallyread directly in bootstrap.

存储在响应对象中的其它变量都从全局数组如提取$_SERVER$_POST或者$_GET您可以格外在自举直接读取。

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

但最有可能的是,您想在前端控制器插件中使用 Response 对象:

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

回答by Chris

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.

如果您需要以某种方式更改应用程序,请使用控制器插件来做到这一点。

回答by Ciaran McNulty

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

您需要先引导 frontController,尝试如下操作:

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