读取控制器中的 HTTP 标头(Zend Framework)
长话短说: 我正在为 Zend Framework 构建一个框架应用程序,并且已经到了需要设置 api
模块的部分。我使用 Zend_Rest_Controller
来完成这项工作。到目前为止一切都很好,我需要在控制器中获取 HTTP 标头来验证 api 密钥。
在我在网上读到的各种教程中,事情是通过前端控制器插件完成的,但我需要它比这更“即插即用”(每次检查应用程序的配置,决定哪个模块是 api等等)。
我尝试了看起来最明显的 $this->getRequest()->getHeaders()
但似乎不起作用,至少对于我将在其中使用 api 的 HTTP 标头不起作用钥匙。 reponse
对象都不是。
谁能帮我解决这个问题吗?
Long story short:
I'm building a skeleton application for Zend Framework and I got to the part where I need to setup the api
module. I'm using Zend_Rest_Controller
for this job. All is ok up to this part where I need to get the HTTP headers in a controller to verify the api key.
On various tutorials I've read on the web the thing is done via a front controller plugin, but I need it to be more "plug and play" than that (checking each time the config of the application, deciding which module is the api and so on).
I tried what seemed most obvious $this->getRequest()->getHeaders()
but doesn't seem to work, at least not for the HTTP headers where I'll be seding my api key. Neither the reponse
object.
Can anyone help me with this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Bogdan 所说,您可以在 Zend_Controller_Request_HTTP 类中找到该信息。可以通过执行以下操作在控制器本身中找到它:
不幸的是,您无法一次访问所有标头,但 ZF 所做的只是使用 apache_request_headers() 函数(如果服务器上可用)来获取它们。
As Bogdan said, you can find that information in the Zend_Controller_Request_HTTP class. It can be found in the controller itself by doing :
Unfortunatly, you can't access all headers at once but what ZF does is just use apache_request_headers() function if available on the server to get them.
毕竟我找到了一种方法:)
在控制器中的
preDispatch()
方法上,您可以执行以下操作:似乎
Zend_Controller_Request_Http
对象使您可以访问标头。有关Zend_Controller_Request_Http
的更多信息,您可以在此处< /a>I found a way of doing this after all :)
On the
preDispatch()
method in your controller you can do the following:It seems that
Zend_Controller_Request_Http
object gives you acces to the headers. More info on theZend_Controller_Request_Http
you can find here