dongyan6235 2014-11-20 11:36
浏览 18
已采纳

Yii - 使缩小可配置的最佳方法

I am using minscript extension for Javascript and CSS minification on my site. I have the following code in my protected/config/main.php

'clientScript'=>array(
    'class'=>'ext.minScript.components.ExtMinScript',
)

In one of the modules I need custom implementation of CClientScript class. So I made a new class and derived it from ExtMinScript, like this.

class BaseClientScript extends ExtMinScript

In the module initialization code, I use setComponent to "overwrite" the "clientScript" component from main configuration. Thus I get the minification implementation from ExtMinScript and my implementation in BaseClientScript.

If I want to disable minification in the module, I change the code to derive BaseClientScript from CClientScript.

class BaseClientScript extends CClientScript

Now I want minification to be configurable in the main configuration. (protected/config/main.php)

I can make two classes, one deriving from CClientScript and other from ExtMinScript. (BaseClientScript and BaseMinClientScript) I don't want to do that, as that would mean that I have duplicate my custom client-script implementation in two classes.

Is there a better way to handle this situation?

  • 写回答

1条回答 默认 最新

  • duanchao4445 2014-11-21 04:11
    关注

    Use a technique like this:

     define('MinClientScriptClass', 'CClientScript');
    

    when you want to use minification in protected/config/main.php

    and write this in BaseClientScript:

    if (defined('MinClientScriptClass')) {
    
        class MinClientScriptClass extends CClientScript {}
    
    } else {
    
        class MinClientScriptClass extends ExtMinScript {}
    }
    
    class BaseClientScript extends MinClientScriptClass
    

    NOTE: When you want to disable minification just comment the define in main config. Or use config params in main.php, and config true/false value check in if above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂