Bật components ở common\config\main.php
'cache' => [ 'class' => 'yii\caching\FileCache', ],
Bước 2 :
Cấu hình ở controller
<?php namespace frontend\controllers; use Yii; use yii\web\Controller; /* * author : Nguyễn Anh Hiến * facebook : https://www.facebook.com/hienk8c * email : anhhienbadao@gmail.com */ class PagecacheController extends Controller { public function behaviors() { return [ 'pageCache' => [ 'class' => 'yii\filters\PageCache', 'only' => ['index'], // số s cache 'duration' => 30, 'variations' => [ \Yii::$app->language, ] ], ]; } public function actionIndex() { $var = "Heheh"; return $this->render('index',['data'=>$var]); } }
File index.php
<h2>Xin chào cache <?= $data?></h2>
Video demo