3

I am using Zend-Framework 2 and I want to access request headers in the Module for authentication purpose. How can I access the header in module.php?

I have tried this all

$headers = $this->getRequest()->getHeaders();
$headers = $this->getRequest()->getHeader('userId');
$headers = $this->getRequest()->userId;
$headers = $_SERVER['HTTP_LOGIN'];

I have also tried this

$headers = apache_request_headers();

It's working but I need something else How to get it in module.php? Thank you.

1 Answer 1

2

A very dirty solution just to show you how to get access to your request headers. I would NOT recommend attaching authentication to this directly.

public function onBootstrap(MvcEvent $event)
{
    $headers = $event->getRequest()->getHeaders();
    var_dump($headers);
}

I would rather suggest attaching a listener to your EventManager on MvcEvent::EVENT_ROUTE event and take it from there.

You can easily pull the Request object from your MvcEvent with the getRequest() method inside your custom Authentication listener.

Check the ZF2 documentation for details on the EventManager and attaching listeners.

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

Comments

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.