Magento has got it’s own methods to manipulate cookies. So we will not use PHP functions.
Create a cookie
We will create a test controller to create our cookie :
app/code/Maxime/Jobs/Controller/Cookie/Testaddcookie.php
Put this code inside :
<?php namespace Maxime\Jobs\Controller\Cookie; class Testaddcookie extends \Magento\Framework\App\Action\Action { const JOB_COOKIE_NAME = 'jobs'; const JOB_COOKIE_DURATION = 86400; // lifetime in seconds /** * @var \Magento\Framework\Stdlib\CookieManagerInterface */ protected $_cookieManager; /** * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory */ protected $_cookieMetadataFactory; /** * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager * @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory */ public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory ) { $this->_cookieManager = $cookieManager; $this->_cookieMetadataFactory = $cookieMetadataFactory; parent::__construct($context); } public function execute() { $metadata = $this->_cookieMetadataFactory ->createPublicCookieMetadata() ->setDuration(self::JOB_COOKIE_DURATION); $this->_cookieManager->setPublicCookie( self::JOB_COOKIE_NAME, 'MY COOKIE VALUE', $metadata ); echo('COOKIE OK'); } }
You can inject the CookieMetadataFactory and the cookieManager inside the class you want with the construct method. We put it inside a controller to make some tests easily.
Now you can test the controller :
http://magento2.lan/jobs/cookie/testaddcookie
If you check your cookies, you will see the one we created :
It’s lifetime is 24 hours (86400 seconds).
You can also set the path and domain of your cookie :
$metadata = $this->_cookieMetadataFactory ->createPublicCookieMetadata() ->setPath('YOUR PATH') ->setDomain('YOUR DOMAIN') ->setDuration(self::JOB_COOKIE_DURATION);
Read a cookie
To read the cookie, we will use another controller :
app/code/Maxime/Jobs/Controller/Cookie/Testgetcookie.php
With this code :
<?php namespace Maxime\Jobs\Controller\Cookie; class Testgetcookie extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Framework\Stdlib\CookieManagerInterface */ protected $_cookieManager; /** * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager */ public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager ) { $this->_cookieManager = $cookieManager; parent::__construct($context); } public function execute() { $cookieValue = $this->_cookieManager->getCookie(\Maxime\Jobs\Controller\Cookie\Testaddcookie::JOB_COOKIE_NAME); echo($cookieValue); } }
We don’t have to use a CookieMetadata, so we only need a cookieManager.
If you go on the page :
http://magento2.lan/jobs/cookie/testgetcookie
You will see the cookie value.
Delete a cookie
We will delete the cookie we created :
app/code/Maxime/Jobs/Controller/Cookie/Testdeletecookie.php
Put this code inside :
<?php namespace Maxime\Jobs\Controller\Cookie; class Testdeletecookie extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Framework\Stdlib\CookieManagerInterface */ protected $_cookieManager; /** * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager */ public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager ) { $this->_cookieManager = $cookieManager; parent::__construct($context); } public function execute() { $this->_cookieManager->deleteCookie( \Maxime\Jobs\Controller\Cookie\Testaddcookie::JOB_COOKIE_NAME ); echo('COOKIE DELETED'); } }
You can also define the cookie domain and cookie path to delete if you define it during the creation. If you want to do this, you have to inject a cookieMetadataFactory inside the class, and use this method to delete :
$this->_cookieManager->deleteCookie( \Maxime\Jobs\Controller\Cookie\Testaddcookie::JOB_COOKIE_NAME, $this->_cookieMetadataFactory ->createCookieMetadata() ->setPath('YOUR PATH') ->setDomain('YOUR DOMAIN') );
Unclean method to use cookies
You can use the objectManager to retrieve the cookieManager and create a cookie where you want :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cookieManager = $objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface'); // set cookie value $cookieManager->setPublicCookie('unclean_cookie', 'Unclean Value'); //get cookie value $cookieManager->getCookie('unclean_cookie');
This method is not recommended !