10

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?

2 Answers 2

21

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

Sign up to request clarification or add additional context in comments.

1 Comment

I've dug deep into the annals of Zend history and found this nugget. This totally saved my butt today. Thank you good sir, thank you.
10

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.

2 Comments

When I run this from a subclass of Mage_Customer_AccountController the result is a fatal error: Call to undefined method Namespace_Module_AccountController::getFrontController() . I'm using Magento v 1.9.1.0.
@quickshiftin - of course - because these are methods of Zend Framwork and Magento doesn't have them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.