José Carlos
José Carlos

Reputation: 2922

ZF2 read HTTP Cookie

I have a file (caller.php) which create a cookie in HTTP response and then redirect to a controller in a ZF2 application (LoginController).

caller.php

setcookie("_ga", "GA1.2.1622977711.1433494392");
setcookie("_gat", "1");

header("Location: http://gnsys.local/publico/login");

LoginController

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Http\Response;

class LoginController extends BasePublicController{

public function indexAction(){

    $response = $this->getResponse();

    foreach ($response->getHeaders() as $header) {
        echo "Campo: " . $header->getFieldName() . ' with value ' . $header->getFieldValue() . "<br />";
    }


    return new ViewModel();
}
}

When LoginController is called, I haven't got any header from the HTTP response.

What am I doing wrong? Why I can't read any value from my http response headers? How can I read all the headers from HTTP Response?

If I do what I want but just only using PHP, LoginController is changed by a file called login.php whith this code:

foreach (getallheaders() as $name => $value) {
    echo "$name: $value</br>";
}

And this code works fine and give me what I want. How can I get the same in Zend Framework 2?

Upvotes: 3

Views: 2438

Answers (3)

Stephane
Stephane

Reputation: 12740

It took me a while to get the cookie setting and reading correctly on a Zend Framework 2 environment.

I created these two functions in a controller:

private function setCookie($name, $value) {
    $cookie = new SetCookie($name, $value, time() + 3600, '/');
    $this->getResponse()->getHeaders()->addHeader($cookie);
}

private function getCookie($name) {
    $cookie = $this->getRequest()->getCookie();
    if ($cookie->offsetExists($name)) {
        return $cookie->offsetGet($name);
    }
}

And I use them to keep a form field value across reloads as in:

$email_search = $this->params()->fromPost('email_search', '');
$is_submitted = $this->params()->fromPost('is_submitted', '');
if ($is_submitted) {
    $this->setCookie('user_list_email_search', $email_search);
} else {
    $email_search = $this->getCookie('user_list_email_search');
}
$form->get('email_search')->setValue($email_search);

The is_submitted form field is a static hidden flag:

<input type="hidden" name="is_submitted" value="1">

It is to allow the operator to reset to blank the email field.

There is a simpler alternative possible, in checking for a form filed being set or not, this time without any is_submitted hidden field:

$posts = $this->params()->fromPost();
if (isset($posts['email_search'])) {
    $email_search = $posts['email_search'];
    $this->setCookie('user_list_email_search', $email_search);
} else {
    $email_search = $this->getCookie('user_list_email_search');
}
$form->get('email_search')->setValue($email_search);

It all rests on the isset($posts[ construct.

Upvotes: 2

mikicaivosevic
mikicaivosevic

Reputation: 157

You can use my zf2-cookie plugin controller module to read/write cookies.

Installation:

composer require mikica/zf2-cookie

You need to register new module. Add in file config/application.config.php:

'modules' => array( '...', 'ZfCookie' ),

Usage in controller

$this->cookie('key'); //read cookie

$this->cookie('key','value'); //write cookie

Upvotes: 0

Stafox
Stafox

Reputation: 1005

You should extract headers from request, but not response:

foreach ($this->getRequest()->getHeaders() as $header) {
   echo 'Campo: ' . $header->getFieldName() . ' with value ' . $header->getFieldValue() . '<br />';
}

To get cookies only use next:

foreach ($this->getRequest()->getCookie() as $name => $value) {
    echo 'Campo: ' . $name . ' with value ' . $value . '<br />';
}

Set cookie from action:

public function callerAction()
{
    $cookie = new \Zend\Http\Header\SetCookie();
    $cookie->setName('foo')
        ->setValue('bar')
        ->setDomain('gnsys.local')
        ->setPath('/')
        ->setHttponly(true);

    /** @var \Zend\Http\Response $response */
    $response = $this->getResponse();
    $response->getHeaders()->addHeader($cookie);

    return $this->redirect()->toUrl('/publico/login');
}

Upvotes: 4

Related Questions