Add version number to images in symfony to avoid browser caching

I would like to avoid browser caching on my images by adding the SVN version number after each image like this (in the same way as this announcement ):

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

      

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

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

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

      

+3


source to share


1 answer


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

It has been canceled since then, don't know why ... (intrusive?).

Override the default helper

Anyway, I think you need to create your own AssetHelper (copied all content from the current one) and add and configure patch # 6135 in lib/helper/CustomAssetHelper.php

.

But you cannot unload the AssetHelper because it is automatically loaded into the kernel: http://trac.symfony-project.org/browser/branches/1.4/lib/view/sfPHPView.class.php#L33 So there will be a conflict, since you have double function (in AssetHelper and CustomAssetHelper).

Add custom template engine

The idea is for a custom sfPHPView

override loadCoreAndStandardHelpers

to call your own resource 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 file, you need to add module.yml

in config/

or apps/frontend/config/

with the following content ( inspired by sfTwigPlugin ):

all:
  view_class: sfCustom

      

Cancel all image_tag()

As Izmir Ramirez said, image_tag()

calling image_path()

, calling _compute_public_path($source, 'images', 'png', $absolute);

.

In the function _compute_public_path

before the last condition, you set up the query_string to add your own version number (which will define somewhere else - like sfConfig):

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

      

It can be a little tricky, but using this method, you can override the image_tag function and add the desired version number without overriding the entire image_tag () call.

About the image inside the CSS, this is a bit tricky as you will have to parse the css or write the css in PHP. Not sure how best to do it.

+1


source







All Articles