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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With