ZF2 set cookie inside the module

I am trying to set a cookie at the second page request inside the module.php. My page request session setter works well but somehow the cookie isn't set. Is there an explanation why this cookie isn't set?

Module.php

    public function onBootstrap(MvcEvent $e)
    {
        $application          = $e->getApplication();
        $eventManager        = $application->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener ->attach($eventManager);
        $serviceManager      = $application->getServiceManager();
        //..
        if (php_sapi_name() !== 'cli') {
            //....
            $this->_setUserTrackingCookie($serviceManager);
        }
    }

    private function _setUserTrackingCookie($serviceManager)
    {
        $request  = $serviceManager->get('Request');
        $headers  = $request->getHeaders();
        $cookies  = $request->getCookie();
        if (!$cookies->offsetExists('fzz')) {
            $acceptingCookies = new Container('fzz');
            if (!$acceptingCookies->offsetExists('requests')) {
                $acceptingCookies->offsetSet('requests', 1);
            } else {
                $cookieSalt          = Rand::getString(32, 'abcdefghijklmnopqrstuvwxyz1234567890', true);
                $dbAdapter           = $serviceManager->get('Zend\Db\Adapter\Adapter');
                $table               = new \Application\Model\UserCookieTable($dbAdapter);
                $userCookie          = new UserCookie();
                $userCookie->salt    = $cookieSalt;
                $userCookie->country = $country;
                $table               ->insertUserCookie($userCookie);
                $locationCookie      = new SetCookie('fzz', 'testing_cookie', strtotime('+1 Year', time()), '/');
                $headers             ->addHeader($locationCookie);
            }
        }
    }

Answers


Triming down your code a little bit, the cookie setting part reads as:

$request  = $serviceManager->get('Request');
$headers  = $request->getHeaders();
$locationCookie = new SetCookie('fzz', 'testing_cookie', strtotime('+1 Year', time()), '/');
$headers->addHeader($locationCookie);

You're adding a a cookie to the Representation of the Request. While this works quite fine for the representation, we cannot actually modify the request of course.

What you actually want to do is to add the Cookie header to the Response instead:

$response = $serviceManager->get('Response');
$headers  = $response ->getHeaders();
$locationCookie = new SetCookie('fzz', 'testing_cookie', strtotime('+1 Year', time()), '/');
$headers->addHeader($locationCookie);

There is by the way a more Zf-way of checking for an actual HTTP request:

if ($request instanceof \Zend\Http\PhpEnvironment\Request) {
    $headers  = $response ->getHeaders();
    $locationCookie = new SetCookie('fzz', 'testing_cookie', strtotime('+1 Year', time()), '/');
    $headers->addHeader($locationCookie);
}

On the CLI, $request will be an instance of Zend\Console\Request instead.


Need Your Help

PyOpenGL: glVertexPointer() offset problem

opengl rendering pyopengl

My vertices are interleaved in a numpy array (dtype = float32) like this: ... tu, tv, nx, ny, nz, vx, vy, vz, ...

Setting the value of a select2 input with jquery

javascript jquery jquery-select2

I am attempting to set a select2 value using jquery. This is likely a duplicate question but I have read 30 different responses on here and haven't found one that solves my problem.