I have this code:
$this->assets
->collection('header')
->setTargetPath('css/final.css')
->setTargetUri('css/final.css')
->addCss('css/base/base.css')
// Use the built-in Cssmin filter
->addFilter(new \Phalcon\Assets\Filters\Cssmin);
what happens is that, when requesting the page, there are 2 identical requests for the same final.css file, but the html source code only has 1 link for the css file.
Any help would be appreciated.
This happens when a dispatch forward has been done after a 'beforeDispatch'; your base controller will get initialized twice. If you look at the dispatchLoop if the controller has been initialized then your assets will be compiled. If you then forward to another controller, that controller will get initialized and your assets will be compiled again.
There are a couple solutions that I use: either put an isInitialized flag and pass that along with your forward as a route parameter on your assets to prevent it from initializing again, or move asset compilation to a service on the dependencyInjector. I think the docs for phalcon don't really make clear that your assets will get compiled every time if you follow their baseController example. Most of us use the asset manager as a static "compile all" where as the baseController method allows for dynamic inclusion of assets-- which seems like a good idea but its usually fine just to minify all resources once and send it along as a static file, in which case assets in baseController is overhead.