PHP code example of narekmarkosyan / yii2-html-cache

1. Go to this page and download the library: Download narekmarkosyan/yii2-html-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

narekmarkosyan / yii2-html-cache example snippets


    'htmlcache' => [
        'class' => '\narekmarkosyan\htmlcache\HtmlCache',
        'lifeTime' => 60*60*24, // 1 day in seconds
        'extra_params' => [],
        'disabled' => false,
        'excluded_actions' => [],
        'excluded_params' => [],
    ],

Yii::$app->htmlcache->loadFromCache($this, $action);

/**
 * @param CInlineAction $action
 * @return bool
 */
protected function beforeAction($action)
{
    Yii::$app->htmlcache->loadFromCache($this, $action);
 
    return parent::beforeAction($action);
}

$output = Yii::$app->htmlcache->saveToCache($this, $this->action, $output);

/**
  * @param string $output
  * @return string
 */
 public function renderContent($content){
    $content = parent::renderContent($content);
    
    $content  = Yii::$app->htmlcache->saveToCache($this, $this->action, $content);

    return $content;
}

    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")));

    Yii::$app->htmlcache->excludeActions($this, "my_action");
    
    // OR
    
    Yii::$app->htmlcache->excludeActions($this, array("my_action", "my_other_action"));

    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

    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

    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