Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get header values in Module.php?

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.

like image 725
SANDHYA GOR Avatar asked May 22 '26 15:05

SANDHYA GOR


1 Answers

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.

like image 193
Wilt Avatar answered May 25 '26 17:05

Wilt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!