Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read HTTP headers in Controller (Zend Framework)

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?

like image 637
Bogdan Constantinescu Avatar asked Jun 22 '10 10:06

Bogdan Constantinescu


People also ask

How to retrieve data from HTTP request in Zend Framework 3?

Retrieving Data from HTTP Request In a controller's action method, you may need to retrieve the data from the HTTP request (the data like GET and POST variables, cookies, HTTP headers and so on). For this purpose, Zend Framework 3 provides you with Zend\Http\Requestclass, which is part of Zend\Httpcomponent.

How to read HTTP headers in a spring controller?

Let’s see how to read HTTP Headers in a Spring Controller. We will look at the following 2 options to access the header information: Read individual HTTP headers. To read individual HTTP header in Spring, we can use the @RequestHeader annotation and specify the header name as the parameter.

How do I get a specific header from a request?

Individually If we need access to a specific header, we can configure @RequestHeader with the header name: Then we can access the value using the variable passed into our method. If a header named accept-language isn't found in the request, the method returns a “400 Bad Request” error.

What happens if the header is not found in the request?

If the header isn't found in the request, the controller returns a 400 error. Let's use the required attribute to indicate that our header isn't required:


2 Answers

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

like image 105
Bogdan Constantinescu Avatar answered Sep 20 '22 00:09

Bogdan Constantinescu


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.

like image 42
jhuet Avatar answered Sep 18 '22 00:09

jhuet