Benjamin Crouzier
Benjamin Crouzier

Reputation: 41895

Add a revision number to images in symfony to avoid browser cache

I would like to avoid browser cache on my images by appending the SVN revision number after each images like this (in the same fashion than this answser):

<?php $v = getRevisionNumber() ?>
<img src="picture.jpg?v=<?= $v ?>" alt="">

Is there a way to do it automatically in Symfony 1.4 (like this for js/css, but with images instead)

Also, how can I do it for an image that is in a css file ?

#title {
    background-image: url(/images/title.png);
}

Upvotes: 3

Views: 887

Answers (1)

j0k
j0k

Reputation: 22756

I found something interesting in the symfony tracker, for the version 1.3/1.4, there were a patch to automatically add a timestamp to all files in the web directory: http://trac.symfony-project.org/ticket/6135

It has been reverted since, no idea why ... (to intrusive?).

Override the default asset helper

Anyway, I think you have to create your own AssetHelper (copied all contents from the current one) and add & customize the patch #6135 into a lib/helper/CustomAssetHelper.php.

But you can't unload the AssetHelper because it is automatically loaded in the core: http://trac.symfony-project.org/browser/branches/1.4/lib/view/sfPHPView.class.php#L33 So there will be conflict since you will have duplicate function (in AssetHelper and CustomAssetHelper).

Add a custom template engine

The idea is to have a custom sfPHPView to redefine the loadCoreAndStandardHelpers to call your own asset helper (put it in lib/view/sfCustomPHPView.class.php):

class sfCustomPHPView extends sfPHPView
{
  /**
   * Loads core and standard helpers to be use in the template.
   */
  protected function loadCoreAndStandardHelpers()
  {
    static $coreHelpersLoaded = 0;

    if ($coreHelpersLoaded)
    {
      return;
    }

    $coreHelpersLoaded = 1;

    $helpers = array_unique(array_merge(array('Helper', 'Url', 'CustomAsset', 'Tag', 'Escaping'), sfConfig::get('sf_standard_helpers')));

    // remove default Form helper if compat_10 is false
    if (!sfConfig::get('sf_compat_10') && false !== $i = array_search('Form', $helpers))
    {
      unset($helpers[$i]);
    }

    $this->context->getConfiguration()->loadHelpers($helpers);
  }
}

To change the default sfPHPView, you need to add a module.yml in config/ or apps/frontend/config/ with the following contents (inspired from sfTwigPlugin):

all:
  view_class: sfCustom

Override all image_tag()

As Yzmir Ramirez said, image_tag() calls image_path() which call _compute_public_path($source, 'images', 'png', $absolute);.

In _compute_public_path function, before the last condition, you customize the query_string to add your own revision number (which will be define somewhere else - sfConfig for example):

$file = sfConfig::get('sf_web_dir').$source;
if ('images' == $dir && sfConfig::get('my_revision_number'))
{
  $query_string .= sfConfig::get('my_revision_number');
}

It might be a bit complex but using this way, you can override image_tag function and add the version number you want without redefine all you image_tag() call.

About image inside a CSS, it's a bit more complex since you will have to parse css or write css in PHP. No idea about the best way to do.

Upvotes: 1

Related Questions