Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save an array of data in a cookie with Symfony?

First off, I'm not all that familiar with cookies but I know how they work. I've seen quite a few different tutorials with plain PHP code but I'm looking for a solid example of how to store arrays in a cookie using the symfony syntax:

$this->getResponse()->setCookie('myCookie', $data);

You can't just pass in an array since it expects a string. Is the only way to do it to serialize an array first?

Are there any other options while storing data in a cookie?

like image 537
Justin Avatar asked Jan 24 '23 17:01

Justin


1 Answers

If you really need to store it in a cookie and not in a session, you can use serialization:

$this->getResponse()->setCookie('myCookie', serialize($data));

$data = unserialize($this->getRequest()->getCookie('myCookie'));
like image 95
laurentb Avatar answered Jan 26 '23 06:01

laurentb