zoukankan      html  css  js  c++  java
  • ThinkPHP动态版本控制

      动态版本控制可以根据时间戳来实现,但是这样的话,每次打开页面都会重新下载加了版本控制的文件,如果你的页面自身打开本来就慢的话,

    这样一来,无疑会带来很差的用户体验。

      但是如果在每个引用文件后面都手动加上版本,如果页面比较多的话,这很明显是个很蠢的做法,所以我们可以这样做:

      (1)在入口文件定义公共模块:

                 define('COMMON_PATH','./Common/');

         然后将Home文件夹下的Common剪切出来,放在和入口文件同目录。

      (2)在Common-->Conf-->config.php中配置一个数组:

              <?php
                return array(
                //CSS版本控制
                'cssVersion' => '1.0',
                'cssVersionid' => '1.0',
                //JS版本控制
                'jsVersion' => '1.0',
                'jsVersionid' => '1.0',
                );

         (3)在控制器中引入      

            <?php
              namespace HomeController;
              use ThinkController;
              class IndexController extends Controller {
                public function index(){
                /* 版本控制 */
                $config = require(COMMON_PATH.'Conf/config.php');

                $this->assign('cssVersionid', $config['cssVersionid']);

                $this->assign('jsVersionid', $config['jsVersionid']);
                /* 页面显示 */
                $this->display();
                  }
                }

         当然所有的assign必须放在$this->display();前面。在thinkPHP3.2.3中,你也可以这样写:

            <?php
              namespace HomeController;
              use ThinkController;
              class IndexController extends Controller {   

                  public function __construct(){ /* 版本控制 */

                     parent::__construct();

                       $config = require(COMMON_PATH.'Conf/config.php');

                     $this->assign('cssVersionid', $config['cssVersionid']);

                     $this->assign('jsVersionid', $config['jsVersionid']);
                   }


                   public function index(){
                   /* 页面显示 */
                   $this->display();
                   }
               }

      (4)这时候就可以在HTML页面引入了

             <link rel="stylesheet" type="text/css" href="__PUBLIC__/css/page/index.css?v={$cssVersionid}">

             <script src="__PUBLIC__/js/page/index.js?v={$jsVersionid}"></script>

      这样一来,每次有更新的话,只需要手动在config.php更改版本就行了,而且可以只更新CSS或JS其中一个。

  • 相关阅读:
    在MyEclipse中运行tomcat报错 严重: Error starting static Resources
    MyEclipse 2015 运行tomcat 内存溢出的解决方法
    (转)Tomcat内存设置详解
    Object调用控件的办法
    Hibernate主键生成方式之hilo
    (转)“中国第一程序员” 求伯君的传奇经历
    雷军相识求伯君
    (转)雷军重掌金山幕后:与求伯君暗战三年两次逼宫
    华军软件发展及盈利模式
    中年人编程
  • 原文地址:https://www.cnblogs.com/Man-Dream-Necessary/p/5643079.html
Copyright © 2011-2022 走看看