PHPackages                             tfountain/asset-path - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tfountain/asset-path

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tfountain/asset-path
====================

Simple ZF2 module for asset versioning

062PHP

Since Nov 24Pushed 12y ago1 watchersCompare

[ Source](https://github.com/tfountain/asset-path)[ Packagist](https://packagist.org/packages/tfountain/asset-path)[ RSS](/packages/tfountain-asset-path/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

This module provides an easy way to version static files (images, stylesheets, javascript etc.), which, when combined with some cache-friendly server configuration, can improve the rendering time of your pages.

Installation
------------

[](#installation)

The easiest way to install the module is using Composer (). Add to your `composer.json`:

```
"require": {
    "tfountain/asset-path": "dev-master"
}

```

and then update your application's `application.config.php` to add `TfAssetPath` to your modules array.

You will also need to modify your `mod_rewrite` rules to rewrite versioned asset requests to the non-versioned equivalent. Modify your `.htaccess` file (or vhost), and add:

```
# Versioned assets
RewriteRule ^(images|js|css)/(([\w.-]+/)+)?([\w.-]+)\.[\w]+\.([\w]+)$ $1/$2$4.$5 [L]

```

above the final rewrite block in the file. So if you're using the `.htaccess` file from the Zend Framework Skeleton application, your modified `.htaccess` would look like this:

```
RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# Versioned assets
RewriteRule ^(images|js|css)/(([\w.-]+/)+)?([\w.-]+)\.[\w]+\.([\w]+)$ $1/$2$4.$5 [L]

# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

```

Change `(images|js|css)` to contain a pipe separated list of static file folders you might want to use this module on (i.e. folders you have in your `public` folder).

To see any benefit, you'll need to serve these files with far in the future 'Expires' headers. If you're using Apache, the easiest way to do this is using `mod_expires`. Either modify your vhost (preferred) to add a `` block for each folder:

```

        ExpiresActive On
        ExpiresByType text/css "access plus 60 days"

```

or add a `.htaccess` file to that folder:

```

    ExpiresActive On
    ExpiresByType text/css "access plus 60 days"

```

Usage
-----

[](#usage)

The module provides a view helper that will, for a given file path, return the same path with a versioning hash inserted just before the file extension. Use the helper in place of situations where you would otherwise put paths to static files, e.g.:

```
