Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Last active December 24, 2015 23:09
Show Gist options
  • Select an option

  • Save clouddueling/6877913 to your computer and use it in GitHub Desktop.

Select an option

Save clouddueling/6877913 to your computer and use it in GitHub Desktop.
force load js and css with auto versioning
// Concept
public static function auto_version($url)
{
$file = $_SERVER['DOCUMENT_ROOT'] . '/' . $url;
if(! is_file($file))
return $url;
$mtime = filemtime($file);
return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", URL::to_asset($url));
}
// from L3 /laravel/html.php
public static function script($url, $attributes = array())
{
$url = static::auto_version($url);
return "<script src='{$url}' " . static::attributes($attributes) . "></script>" . PHP_EOL;
}
public static function style($url, $attributes = array())
{
$defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
$attributes = $attributes + $defaults;
$url = static::auto_version($url);
return "<link href='{$url}' " . static::attributes($attributes) . ">" . PHP_EOL;
}
{{ HTML::script("js/whatever.js") }}
outputs:
https://whatever.com/js/whatever.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment