PHP Classes

Legato PHP Asset Management: Get versioned URLs for CSS, JavaScript and images

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 77 All time: 10,162 This week: 455Up
Version License PHP version Categories
legato-asset 1.0.0MIT/X Consortium ...5.3.9HTML, PHP 5
Description 

Author

This package can get versioned URLs for CSS, JavaScript and images.

It can take the name of a file to be served as part of a Web page like an image, CSS or JavaScript and returns a URL to be used in the page to refer to those asset files.

The class serve the specified asset files from the generated URLs.

Those URLs include a parameter with a value that may be passed to the package explicitly or be generated by it using a random value.

Picture of Ogbemudia Osayawe
  Performance   Level  
Innovation award
Innovation award
Nominee: 5x

 

Example

<?php

if(!file_exists('asset')){

   
/**
     * Get versioned url for assets
     *
     * @param $path
     * @param $query
     * @return string
     */
   
function asset($path, $query = null)
    {
       
$version = is_null($query) ? "%s?%s" : "%s?$query=%s";
       
$random = md5('random');

       
$strategy = new \Legato\Framework\src\asset\StaticVersionStrategy(
           
$random, $version
       
);

        return
$strategy->url($path);
    }
}


Details

Legato Asset

A simple tool for generating unique versioned URLs for images, stylesheet, javascript, etc.

Installation

composer require legato/asset

Usage


Relative Path
<link href="<?= asset('css/style.css') ?>" rel="stylesheet">

Absolute Path
<link href="<?= asset('/css/style.css') ?>" rel="stylesheet">

Relative Path Output

css/style.css?7ddf32e17a6ac5ce04a8ecbf782ca509

<link href="css/style.css?7ddf32e17a6ac5ce04a8ecbf782ca509" rel="stylesheet">

Absolute Path Output

/css/style.css?7ddf32e17a6ac5ce04a8ecbf782ca509

<link href="/css/style.css?7ddf32e17a6ac5ce04a8ecbf782ca509" rel="stylesheet">

Specifying A query String


<link href="<?= asset('css/style.css', 'v1') ?>" rel="stylesheet">

css/style.css?v1=7ddf32e17a6ac5ce04a8ecbf782ca509

<link href="css/style.css?v1=7ddf32e17a6ac5ce04a8ecbf782ca509" rel="stylesheet">


  Files folder image Files (10)  
File Role Description
Files folder imageexample (1 file, 2 directories)
Files folder imagesrc (2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:77
This week:0
All time:10,162
This week:455Up