views:

235

answers:

1

After reading this thread: http://stackoverflow.com/questions/118884/what-is-an-elegant-way-to-force-browsers-to-reload-cached-css-js-files

I would like to know if there is any built-in function or easy way in Symfony that automatically forces a reload by appending a random querystring or timestamp to the link when it has discovered that javascript / css file has been modified. (Normally, people use the use_javascript function to generate the <script> tag)

+1  A: 

There is no built-in mechanism, but a little creativity means you can do this just about anywhere in your code, from view.yml to layout.php to each individual action.

The view.yml method is easy enough:

apps/frontend/config/view.yml:

  stylesheets:    [main?v=<?php echo time() ?>, reset?v=<?php echo time() ?>, layout?v=<?php echo time() ?>]

Although I think this is a little too active, and I tend to use either the SVN revision or a overall project version number:

  stylesheets:    [main?v=<?php echo sfConfig('app_project_version') ?>, reset?v=<?php echo sfConfig('app_project_version') ?>, layout?v=<?php echo sfConfig('app_project_version') ?>]

where app_project_version is set in apps/frontend/config/app.yml. Methods for layout.php and actionSuccess.php should be easy enough from here:

<?php use_stylesheet('blah?v='.sfConfig::get('app_project_version')); ?>
Raise
But this would only have effects when using include_javascript / include_stylesheet. In my Symfony application, view.yml is not used at all, the use_javascript and use_stylesheet functions are used instead.
bobo
The principle is the same - these functions are practically synonyms for each other, just used in different contexts (YAML and PHP).In your case, simply use: `<?php use_stylesheet('blah?v='.sfConfig::get('app_version')); ?>`I have updated the answer with this solution.
Raise
Thanks for your help. In the end, I adopted your idea and modified the javascript_path and stylesheet_path functions in AssetHelper.php of the framework to make this possible. But I would have to keep this in mind when doing the version upgrade.
bobo