thinkphp Dynamic Version Control

Source: Internet
Author: User

Dynamic versioning can be implemented based on timestamps, but in that case, the version-controlled file is re-downloaded every time the page is opened, and if your page itself is open, it will be slow.

This will undoubtedly result in a poor user experience.

But if you manually add a version after each reference file, it's obviously a stupid thing to do if the page is more, so we can do this:

(1) Define the public module in the Portal file:

Define (' Common_path ', './common/');

Then cut out the common in the home folder and place it in the same directory as the portal file.

(2) Configure an array in common-->conf-->config.php:

<?php
Return Array (
CSS version control
' Cssversion ' = ' 1.0 ',
' Cssversionid ' = ' 1.0 ',
JS version control
' Jsversion ' = ' 1.0 ',
' Jsversionid ' = ' 1.0 ',
);

(3) introducing in the controller

<?php
namespace Home\controller;
Use Think\controller;
Class Indexcontroller extends Controller {
Public Function index () {
/* Version control */
$config = require (Common_path. ') Conf/config.php ');

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

$this->assign (' Jsversionid ', $config [' Jsversionid ']);
/* Page Display */
$this->display ();
}
}

Of course all assign must be placed in $this->display (); front. In thinkPHP3.2.3, you can also write:

<?php
namespace Home\controller;
Use Think\controller;
Class Indexcontroller extends Controller {

Public Function __construct () {/* Versioning control */

Parent::__construct ();

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

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

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


Public Function index () {
/* Page Display */
$this->display ();
}
}

(4) The HTML page allows you to introduce

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

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

This way, every time there is an update, you only need to manually change the version on the config.php, and can only update CSS or JS one of them.

thinkphp Dynamic Version Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.