We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Preventing duplicate assets

I set up my assets as shown in https://forum.phalcon.io/discussion/2426/load-assets-to-all-views which works great, unless I use $this->dispatcher->forward which results in the initialize being called on a second controller and adding the assets again. I thought just clearing the asset manager at the start of initialize() would work, but it does not have that functionality.

Is there a simple way to handle this? Or maybe a better location to set up my assets?

Another question along the same line: params set in the view also persist after a forward, $this->view->reset(); does not remove them.

edited Jun '15

"Forward" does not create a new request so objects already loaded in memory aren't reset. Maybe using a private property would help there:

<?php

class ControllerBase extends Controller
{

    private assetsSent = false;

    public function initialize()
    {
        if (!$this->assetsSent) {
          //Javascripts in the header
          $this->assets
              ->collection('headerjs')
              ->addJs('https://static.origos.hu/s/js/modules/jquery-1.11.0.min.js', false)
              ->addJs('https://static.origos.hu/s/js/modules/mustache.min.js', false)
              ->addJs('assets/js/app.js');

          //Css in the header
          $this->assets
              ->collection('headercss')
              ->addCss('assets/css/style.css');

              $this->assetsSent = true;
        }       
    }
    ....
}