paulbunyannet/autoversion

Auto versioning class for front end assets.

1.2.0 2015-10-08 21:41 UTC

This package is auto-updated.

Last update: 2024-03-23 01:16:12 UTC


README

#paulbunyannet/autoversion

Build Status Latest Version

paulbunyannet/autoversion Cache busting mechanisim for front end assets, used with the cache busting mechanism provided by the HTML5 Boilerplate Apache .htaccess configuration.

Installation

In your terminal, just run:

composer require "paulbunyannet/autoversion":"~1.0"

Configuration

This package is framework agnostic, the configuration process is:

// Auto-load composer packages
use Pbc\AutoVersion\AutoVersion;
require 'vendor/autoload.php';

// Create new AutoVersion object and configure the document root
$auto = new AutoVersion($_SERVER['DOCUMENT_ROOT']);

Add to your .htaccess file, before any other routing mod rewrites

<IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^(.+)\.(\d+)\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$ $1.$3 [L]
</IfModule>

Usage

In your views, just call:

// $pathToAsset is relative to the document root configured above, 
$auto->file($pathToAsset);

for example:

<link rel="stylesheet" href="<?=$auto->file('/css/main.css') ?>">
<script src="<?=$auto->file('/js/main.js') ?>"></script>

which will output file names with their modified time appended to file name:

<link rel="stylesheet" href="/css/main.1234567890.css">
<script src="/js/main.1234567890.js"></script>