over 3 years ago

1.在 component 寫 ClientScript class去繼承 CClientScript

2.覆寫registerScriptFile、registerCssFile、registerCssFile...etc.

protected/components/ClientScript
<?php
class ClientScript extends CClientScript
{
    private $_baseUrl;
    
    public function registerCssFile($url, $media='')
    {
        $currentDateTime = date('Ymdd').rand(0, 9);
        $url .= (strpos($url, '?') === false) ? '?' : '&';
        $url .= "v={$currentDateTime}";
        
        $this->hasScripts=true;
        $this->cssFiles[$url]=$media;
        $params=func_get_args();
        $this->recordCachingAction('clientScript','registerCssFile',$params);
        return $this;
    }
}

3.config指定ClientScript

<?php
    'components'=>array(
        'clientScript' => array(
            'class'=>'ClientScript',
        ),
   )

4.在view上直接使用

protected/views/layouts/layout
<?php Yii::app()->clientscript->registerCssFile('/css/bootstrap.css'); ?>

結果:

<link rel="stylesheet" type="text/css" href="/css/bootstrap.css?v=20150226264" />

這邊只有舉例stylesheet,javascript可以依樣畫葫蘆。

← try...catch問題 MySQL find_in_set →