读取控制器中的 HTTP 标头(Zend Framework)

发布于 2024-09-06 17:20:20 字数 409 浏览 2 评论 0原文

长话短说: 我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无悔心 2024-09-13 17:20:21

正如 Bogdan 所说,您可以在 Zend_Controller_Request_HTTP 类中找到该信息。可以通过执行以下操作在控制器本身中找到它:

$this -> getFrontController() -> getRequest() -> getHeader('Content-Type');

不幸的是,您无法一次访问所有标头,但 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 :

$this -> getFrontController() -> getRequest() -> getHeader('Content-Type');

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.

携余温的黄昏 2024-09-13 17:20:20

毕竟我找到了一种方法:)

在控制器中的 preDispatch() 方法上,您可以执行以下操作:

public function preDispatch()
{
    $request = new Zend_Controller_Request_Http();
    $key = $request->getHeader('x-apikey');
}

似乎 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:

public function preDispatch()
{
    $request = new Zend_Controller_Request_Http();
    $key = $request->getHeader('x-apikey');
}

It seems that Zend_Controller_Request_Http object gives you acces to the headers. More info on the Zend_Controller_Request_Http you can find here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文