一、cookie
protected $_cookieManager;
private $_cookieMetadataFactory;
private $_sessionManager;
public function __construct(
\Magento\Framework\Stdlib\Cookie\PhpCookieManager $cookieManager,
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
\Magento\Framework\Session\SessionManagerInterface $sessionManager
) {
$this->_cookieManager = $cookieManager;
$this->_cookieMetadataFactory = $cookieMetadataFactory;
$this->_sessionManager = $sessionManager;
}
public function execute(\Magento\Framework\Event\Observer $observer){
//第一次访问,还没有cookie记录
if ($this->_cookieManager->getCookie('olight_geoip') === null) {
$metadata = $this->_cookieMetadataFactory
->createPublicCookieMetadata()
->setDuration(86400)
->setPath($this->_sessionManager->getCookiePath())
->setDomain($this->_sessionManager->getCookieDomain());
$this->_cookieManager->setPublicCookie('olight_geoip',1,$metadata);
}
//删除cookie
$this->_cookieManager->deleteCookie('olight_geoip');
}
二、session
session 列表
/magento/module-catalog/Model/Session.php
/magento/module-newsletter/Model/Session.php
/magento/module-persistent/Model/Session.php
/magento/framework/Message/Session.php
/magento/module-customer/Model/Session.php
/magento/module-backend/Model/Session.php
/magento/module-checkout/Model/Session.php
下面,以/magento/module-checkout/Model/Session.php为例
<?php
$checkoutSession = $this->_objectManager->create('Magento\Checkout\Model\Session');
//set session
$sessionName = 'setFatherDayCustomer'.$customerId;
$checkoutSession->$sessionName(true);
//get session
$sessionName = 'getFatherDayCustomer'.$customerId;
$fatherDayCustomer = $checkoutSession->$sessionName();
//unset session
$sessionName = 'unsFatherDayCustomer'.$customerId;
$checkoutSession->$sessionName();
?>