How does javascript / css reload in symfony?

After reading this topic: How to make the browser reload cached CSS / JS files?

I would like to know if there is a built-in function or an easy way in Symfony that automatically forces you to reload by adding a random chain or timestamp to the link when it detects that the javascript / css file has been modified. (Usually people use a function use_javascriptto generate a tag <script>)

+3
source share
2 answers

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

The view.yml method is quite simple:

Applications / interface / configurations / 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 version or the general 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 it is app_project_versioninstalled in applications /frontend/config/app.yml. The layout.php and actionSuccess.php methods should be fairly easy:

<?php use_stylesheet('blah?v='.sfConfig::get('app_project_version')); ?>
+3
source

, , , , view.yml use_stylesheet(). settings.yml, .

`

function include_versioned_stylesheets()
{
    $response = sfContext::getInstance()->getResponse();
    sfConfig::set('symfony.asset.stylesheets_included', true);
    $html = '';
    foreach ($response->getStylesheets() as $file => $options) {
        $filepath = sfConfig::get('sf_web_dir') . '/' .  stylesheet_path($file);
        if(file_exists($filepath)) {
            $file .= '?v=' . filectime($filepath);
        }
        $html .= stylesheet_tag($file, $options);
    }
    echo $html;
}

`

layout.php . , include_stylesheets(), . include_javascripts.

+2

Source: https://habr.com/ru/post/1734425/


All Articles