LoginSignup
1
2

More than 5 years have passed since last update.

指定した日時でcssやjsファイル、画像ファイルを読み替えるヘルパー

Posted at
timer_read.php
<?php
class TimerReadHelper extends HtmlHelper {
    var $now_date_time = null;

    function __construct() {
        $this->now_date_time = time();
    }

    function css($path, $rel = null, $options = array()) {
        if (!empty($options['from_date'])) {
            $from_date_time = strtotime($options['from_date']);
            if ($from_date_time <= $this->now_date_time) {
                $date_time = date('Y-m-d', strtotime($options['from_date']));
                if (file_exists(CSS . $path . $date_time . '.css')) {
                    $path .= $date_time;
                }
            }
        }

        unset($options['from_date']);
        return parent::css($path, $rel, $options);
    }
}
home_controller.php


<?php
class HomeController extends AppController {
    var $name = 'Home';
    var $uses = array();
    var $helpers = array(
        'TimerRead',
    );

    function index() {
    }
}
index.ctp
<?php echo $timerRead->css('test',NULL,array('inline' => false, 'from_date' => '2013-06-03 00:00:00')); ?>

これで、
指定日時以前は「test.css」を、
指定日時を過ぎていて、かつ 「test2013-06-03.css」というファイルが存在したらそれを読み込む。
指定日時を過ぎているが、「test2013-06-03.css」というファイルが無かったら「test.css」を読み込む。
指定日時は、「Y-m-d H:i:s」等、「strtotime()」で解釈出来る形なら何でもいける(と思う)
思いつきの突貫で作ったから、雑な感じ&穴はあるでしょう。
同じようにして「jsファイル」や「画像ファイル」もいけるかな。
それらは後日。

一番の解決策は、
「午前0時更新」なんて物を止める。
(これが今回、本ヘルパーを思いついた要因でもあったり…)

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2