Gelin hep beraber Cookie'lere bir göz atalım: Nasıl oluşturulurlar, nasıl okunurlar, nasıl silinirler ya da kendimize göre nasıl bazı ayarlamalar yapabiliriz.
Set Cookies
Cookie oluşturmak için aşağıdaki kodları kullanabilirsiniz:
<?php
$cookies = Yii::$app->response->cookies;
// add a new cookie to the response to be sent
$cookies->add(new \yii\web\Cookie([
'name' => 'username',
'value' => 'yiiuser',
'expire' => time() + 86400 * 365,
]));
?>
Get Cookies
Cookie çağırmak için aşağıdaki kodları kullanabilirsiniz:
<?php
$cookies = Yii::$app->request->cookies;
// get the cookie value
$username = $cookies->getValue('username');
//return default value if the cookie is not available
$username = $cookies->getValue('username', 'default');
// Check the availability of the cookie
if ($cookies->has('username')){
echo $cookies->getValue('username');
}
?>
Remove Cookies
Cookie silmek için aşağıdaki kodları kullanabilirsiniz:
<?php
$cookies = Yii::$app->response->cookies;
$cookies->remove('username');
unset($cookies['username']);
?>