dev-master
9999999-devYii2 Html Cache
GPL-3.0
The Requires
by Narek Markosyan
cache yii2 html
v1.0.1
1.0.1.0Yii2 Html Cache
GPL-3.0
The Requires
by Narek Markosyan
cache yii2 html
Yii2 Html Cache
Yii2 HTML Cache generates cache file with FULL HTML of your action from DOCTYPE
to </html>
., (*1)
This is an extension for Yii 2.x, if you have Yii 1.x use Yii HTML Cache, (*2)
Run, (*3)
$ php composer.phar require narekmarkosyan/yii2-html-cache "dev-master"
or add, (*4)
"narekmarkosyan/yii2-html-cache": "dev-master"
to require
section of your composer.json
., (*5)
Add following to your components
in configs, (*6)
'htmlcache' => [ 'class' => '\narekmarkosyan\htmlcache\HtmlCache', 'lifeTime' => 60*60*24, // 1 day in seconds 'extra_params' => [], 'disabled' => false, 'excluded_actions' => [], 'excluded_params' => [], ],
Add following code at the beginning of beforeAction
method in your controller, (*7)
Yii::$app->htmlcache->loadFromCache($this, $action);
Where $action
is a first parameter of beforeAction
. Or if you don't have beforeAction
add the following code to your controller, (*8)
/** * @param CInlineAction $action * @return bool */ protected function beforeAction($action) { Yii::$app->htmlcache->loadFromCache($this, $action); return parent::beforeAction($action); }
Add the following code at the end of renderContent
method in your controller, (*9)
$output = Yii::$app->htmlcache->saveToCache($this, $this->action, $output);
Where $output
is a first parameter of renderContent
. Or if you don't have renderContent
add the following code to your controller, (*10)
/** * @param string $output * @return string */ public function renderContent($content){ $content = parent::renderContent($content); $content = Yii::$app->htmlcache->saveToCache($this, $this->action, $content); return $content; }
id_product
, add extra_params => array('id_product')
to configstrue
if disabled, false
if enabled. Default: false
array('controller_id'=> array('action1', 'action2'))
format.NOTE 1: in
excluded_params
you need to store Controller variables, not$_GET/$_POST/$_REQUEST
variables, (*11)NOTE 2: you can add excluded action or parameter from controller by calling
excludeActions
andexcludeParams
methods. See Additional features section, (*12)
There are few additional methods you can use in your controller., (*13)
If you have some parts in your HTML that need to be loaded dynamically in cache you can use placeholders in view and then replace them with directReplace
method., (*14)
There are two ways for parameters, (*15)
$replace_key
- key that needs to be replaced$value
- HTML part that needs to be placed instead of placeholderOR, (*16)
$replace_array
- array with $replace_key => $value
In view, (*17)
<div>{DATE_PLACEHOLDER}</div>
In beforeAction
method of your controller, (*18)
Yii::$app->htmlcache->directReplace("DATE_PLACEHOLDER", date("Y-m-d H:i:s")); // OR Yii::$app->htmlcache->directReplace(array("DATE_PLACEHOLDER"=> date("Y-m-d H:i:s")));
NOTE: placeholder always needs to be UPPERCASE and in {BRACES}, (*19)
Adding action to the list of excluded actions., (*20)
$controller
- Controller of actions$actions
- name of action or list of actionsIn beforeAction
method of your controller, (*21)
Yii::$app->htmlcache->excludeActions($this, "my_action"); // OR Yii::$app->htmlcache->excludeActions($this, array("my_action", "my_other_action"));
Removes action from excluded actions list., (*22)
$controller
- Controller of actions$actions
- name of action or list of actionsIn beforeAction
method of your controller, (*23)
Yii::$app->htmlcache->allowActions($this, "my_action"); // OR Yii::$app->htmlcache->allowActions($this, array("my_action", "my_other_action")); // OR Yii::$app->htmlcache->allowActions($this, null); // Removes all actions of this controller
Adding params to the list of excluded params., (*24)
$controller
- Controller of actions$params
- name of param or list of paramsIn beforeAction
method of your controller, (*25)
Yii::$app->htmlcache->excludeParams($this, "my_action"); // Disable cache if $this->my_action != false // OR Yii::$app->htmlcache->excludeParams($this, array("my_action" => 1)); // Disable cache if $this->my_action == 1 // OR Yii::$app->htmlcache->excludeParams($this, array("my_action" => array(1, 2))); // Disable cache if $this->my_action == 1 OR $this->my_action == 2
Removes params from the list of excluded params, (*26)
$controller
- Controller of actions$params
- name of param or list of paramsIn beforeAction
method of your controller, (*27)
Yii::$app->htmlcache->allowParams($this, "my_action"); // Removes all mentions of my_action of this controller if exsists // OR Yii::$app->htmlcache->allowParams($this, array("my_action" => 1)); // Removes my_action == 1 mention from this controller if exsists // OR Yii::$app->htmlcache->allowParams($this, array("my_action" => array(1, 2))); // Removes my_action == 1 OR my_action == 1 mentions from this controller if exsists // OR Yii::$app->htmlcache->allowParams($this, null); // Removes ALL excluded params of this controller
I'm doing everythink like described but it doesn't work., (*28)
hmm..., (*29)
Yii2 Html Cache
GPL-3.0
cache yii2 html
Yii2 Html Cache
GPL-3.0
cache yii2 html