استفاده از cookie ها در فریمورک Yii
Cookieها این قابلیت را فراهم می کنند که داده ها بتوانند در سراسر requestها قابل دسترسی باشند. در PHP شما می توانید از طریق متغیر $_COOKIE به آنها دسترسی پیدا کنید.فریمورک Yii کوکی را به عنوان یک شی از کلاس yii\web\Cookie در نظر می گیرد. در این فصل، ما چندین متد برای خواندن cookieها بیان می کنیم.
مرحله 1 - متد actionReadCookies را در SiteController ایجاد کنید.
public function actionReadCookies() {
// get cookies from the "request" component
$cookies = Yii::$app->request->cookies;
// get the "language" cookie value
// if the cookie does not exist, return "ru" as the default value
$language = $cookies->getValue('language', 'ru');
// an alternative way of getting the "language" cookie value
if (($cookie = $cookies->get('language')) !== null) {
$language = $cookie->value;
}
// you may also use $cookies like an array
if (isset($cookies['language'])) {
$language = $cookies['language']->value;
}
// check if there is a "language" cookie
if ($cookies->has('language')) echo "Current language: $language";
}
مرحله 2 - برای دیدن Cookieها در action، یک متد به نام actionSendCookies را در SiteController ایجاد کنید.
public function actionSendCookies() {
// get cookies 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' => 'language',
'value' => 'ru-RU',
]));
$cookies->add(new \yii\web\Cookie([
'name' => 'username',
'value' => 'John',
]));
$cookies->add(new \yii\web\Cookie([
'name' => 'country',
'value' => 'USA',
]));
}
مرحله 3 - حالا اگر به آدرس http://localhost:8080/index.php?r=site/send-cookies بروید، متوجه خواهید شد که Cookieها در داخل مرورگر ذخیره شده اند.
در فریمورک Yii، به طور پیش فرض، cookie validation فعال می باشد، که از cookieها در مقابل هر گونه تغییر در سمت client محافظت می کند. hash string از فایل config/web.php هر cookie را نشانه گذاری می کند.
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is
//required by cookie validation
'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
//'showScriptName' => false,
//'enablePrettyUrl' => true,
//'enableStrictParsing' => true,
//'suffix' => '/'
],
'db' => require(__DIR__ . '/db.php'),
],
'modules' => [
'hello' => [
'class' => 'app\modules\hello\Hello',
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
?>
شما می توانید cookie validation را با تنظیم yii\web\Request::$enableCookieValidation روی مقدار false غیرفعال کنید.
این فصل در یک نگاه:
…
سلام . پیشنهاد میکنم حتما دموی دوره رو مشاهده کنید درباره فریم ورک Yii: در پاسخ به سوالات : آیا این فریم ورک سریع است؟ آیا این یک فریم ورک امن است؟ آیا این یک فریم ورک حرفه ای است؟ آیا ا…این فصل در یک نگاه:
…
پیشنهاد میکنیم دمو دوره رو حتما مشاهده کنید با سلام خدمت دوستان عزیز . ابتدا عرض کنم که این دوره برای دوستانی آماده شده که میخوان وب اپلیکیشن های مدرنی رو طراحی بکنن و وارد بازار کار ط…