Created
March 13, 2014 08:35
-
-
Save lequinharay/9524411 to your computer and use it in GitHub Desktop.
CakePHPのHTMLヘルパーで、js/cssを出力するところでいちいちAssets.timestampをオンにしたりオフにしたりするのが面倒なので、HTMLヘルパーを継承して共通部分をヘルパーにした。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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