Skip to content

Instantly share code, notes, and snippets.

@lequinharay
Created March 13, 2014 08:35
Show Gist options
  • Select an option

  • Save lequinharay/9524411 to your computer and use it in GitHub Desktop.

Select an option

Save lequinharay/9524411 to your computer and use it in GitHub Desktop.
CakePHPのHTMLヘルパーで、js/cssを出力するところでいちいちAssets.timestampをオンにしたりオフにしたりするのが面倒なので、HTMLヘルパーを継承して共通部分をヘルパーにした。
<?php
/** リソース出力のキャッシュクリアのために、HTMLヘルパーのメソッドをオーバーライドする
*
* @author lequinharay <lequinharay@gmail.com>
*/
class HtmlResourceCacheHelper extends HtmlHelper {
function css($path, $rel = null, $options = array()) {
return $this->__common('css', $path, $rel, $options);
}
function script($url, $options = array()) {
return $this->__common('script', $url, null, $options);
}
/**
* cssやjsを呼び出す瞬間に、Asset.timestampをforceにして、呼び出しURLの末尾に日付パラメーターを付与する。
*
*/
private function __common($methodName, $path, $rel = null, $options = array()) {
$out = null;
$defaultAssetTimestamp = Configure::read('Asset.timestamp');
Configure::write('Asset.timestamp', 'force');
if($methodName == 'css') {
$out = parent::$methodName($path, $rel, $options);
} elseif($methodName == 'script') {
$out = parent::$methodName($path, $options);
}
Configure::write('Asset.timestamp', $defaultAssetTimestamp);
return $out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment