A cookie is a small file that server embeds in the user’s system which is used to identify a user.
In Yii, each cookie is an object of yii\web\Cookie.
The yii\web\Request (Collection of submitted cookies in a request) and yii\web\Response (Collection of cookies that need to be sent to a user) maintains the collection of cookies via the property named cookies.
Controller deals with the cookies request and response in an application. Hence, cookies should be read and sent in controller.
Setting Cookies
Send cookies to the end users using the following codes.
// get the cookie collection (yii\web\CookieCollection) from the "response" component
$cookies = Yii::$app->response->cookies;
// add a new cookie to the response to be sent
$cookies->add(new \yii\web\Cookie([
'name' => 'name',
'value' => 'sssit',
]));
Getting Cookies
To get a cookie use the following code.
// get the cookie collection (yii\web\CookieCollection) from the "request" component
$cookies = Yii::$app->request->cookies;
// get the cookie value. If the cookie does not exist, return "default" as the default value.
$name = $cookies->getValue('name', 'default');
// an alternative way of getting the "name" cookie value
if (($cookie = $cookies->get('name')) !== null) {
$name = $cookie->value;
}
// you may also use $cookies like an array
if (isset($cookies['name'])) {
$name = $cookies['name']->value;
}
// check if there is a "name" cookie
if ($cookies->has('name')) ...
if (isset($cookies['name'])) ...
Removing Cookies
To remove a cookie, use remove() function of Yii.
$cookies = Yii::$app->response->cookies;
// remove a cookie
$cookies->remove('name');
// equivalent to the following
unset($cookies['name']);
Example:
Let’s see an example to set and show cookie’s value.
Step 1 Add the two actions actionSetCookie and actionShowCookie in the SiteController.php file.
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup'],
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout', 'set-cookie', 'show-cookie'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionSetCookie()
{
$cookies = Yii::$app->response->cookies;
$cookies->add(new \yii\web\Cookie
([
'name' => 'test',
'value' => 'SSSIT Pvt. Ltd.'
]));
}
public function actionShowCookie()
{
if(Yii::$app->getRequest()->getCookies()->has('test'))
{
print_r(Yii::$app->getRequest()->getCookies()->getValue('test'));
}
}
Step 2 Run it on the browser to set the cookie first with following URL,
http://localhost/cook/frontend/web/index.php?r=site/set-cookie

Step 3 Run it on the browser to show the cookie with following URL,
http://localhost/cook/frontend/web/index.php?r=site/show-cookie

Leave a Review