Skip to content

narekmarkosyan/yii-html-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Yii HTML Cache

Yii HTML Cache generates cache file with FULL HTML of your action from DOCTYPE to </html>.

This is an extension for Yii 1.x, if you have Yii 2.x use Yii2 HTML Cache

Instalation

Copy htmlcache folder to your protected/extensions

Add following to your components configs

    'htmlcache' => array(
        'class' => 'ext.htmlcache.HtmlCache',
        'lifeTime' => 60*60*24, // 1 day in seconds
        'extra_params' => array(),
        'disabled' => false,
        'excluded_actions' => array(),
        'excluded_params' => array(),
    ),

Usage

Add following code at the beginning of beforeAction method in your controller

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

/**
 * @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 afterRender method in your controller

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

Where $output is a second parameter of afterRender. Or if you don't have afterRender add the following code to your controller

/**
 * @param string $view
 * @param string $output
 */
public function afterRender($view, &$output){
    $output = Yii::app()->htmlcache->saveToCache($this, $this->action, $output);
}

Settings

  • lifeTime - lifetime of generated cache in seconds. Default: 1 day
  • extra_params - parameters in controller that can affect your final HTML. For example, if you have an action with product description and it variates for different id_product, add extra_params => array('id_product') to configs
  • disabled - true if disabled, false if enabled. Default: false
  • excluded_actions - Actions list that doesn't need to be cached in array('controller_id'=> array('action1', 'action2')) format.
  • excluded_params - Params list that doesn't need to be cached if they exist and are equal to exact value. If value not set then checking should not be a false.

NOTE 1: in excluded_params you need to store Controller variables, not $_GET/$_POST/$_REQUEST variables

NOTE 2: you can add excluded action or parameter from controller by calling excludeActions and excludeParams methods. See Additional features section

Additional features

There are few additional methods you can use in your controller.

directReplace

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.

Params

There are two ways for parameters

  • string $replace_key - key that needs to be replaced
  • string $value - HTML part that needs to be placed instead of placeholder

OR

  • array $replace_array - array with $replace_key => $value

Usage

In view

<div>{DATE_PLACEHOLDER}</div>

In beforeAction method of your controller

    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}

excludeActions

Adding action to the list of excluded actions.

Params

  • CController $controller - Controller of actions
  • string|array $actions - name of action or list of actions

Usage

In beforeAction method of your controller

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

allowActions

Removes action from excluded actions list.

Params

  • CController $controller - Controller of actions
  • string|array $actions - name of action or list of actions

Usage

In beforeAction method of your controller

    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

excludeParams

Adding params to the list of excluded params.

Params

  • CController $controller - Controller of actions
  • string|array $params - name of param or list of params

Usage

In beforeAction method of your 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

allowParams

Removes params from the list of excluded params

Params

  • CController $controller - Controller of actions
  • string|array $params - name of param or list of params

Usage

In beforeAction method of your controller

    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

FAQ

I'm doing everythink like described but it doesn't work.

hmm...