Shahbaz
Shahbaz

Reputation: 3463

zf2 read cookie in module class (Module.php)

have set the cookie in my authentication controller now i wan to get/read cookie in my module class, i have seen the docs to get cookie ZendHttpCookie getter methods but it does not read the cookie however cookie is created succesfully. I have created the cookie in the following way

$cookie = new SetCookie('name','value', $time); 
$response = $this->getResponse()->getHeaders();
$response->addHeader($cookie);

can any one help me to read this cookie in module class

Upvotes: 0

Views: 1453

Answers (1)

Bilal
Bilal

Reputation: 2673

In your init function in Module.php

public function init(\Zend\ModuleManager\ModuleManager $moduleManager) {
        $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            $response = $e->getResponse();
            $thatCookie = $request->getHeaders()->get('Cookie')->cookie_name;
        }

}

Updated part. Using remember me.

                    if ($request->getPost('rememberme') == 1 ) {
                        $this->getSessionStorage()
                             ->setRememberMe(1);
                        //set storage again 
                        $this->getAuthService()->setStorage($this->getSessionStorage());
                    }

Upvotes: 1

Related Questions