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

Asset Collection!

Hey,

I am trying to use Css an Js asset collection in the ControllerBase initialize function. So far everything is fine, but when I try to use the collection in the .volt file I always get the error that the collection is not known in the manager.

Here the code: ControllerBase.php

class ControllerBase extends \Phalcon\Mvc\Controller
{
    /**
     * Execute before the router so we can determine if this is a private controller, and must be authenticated, or a
     * public controller that is open to all.
     *
     * @param Dispatcher $dispatcher
     * @return boolean
     */
    public function initialize()
    {
        $this->assets
             ->collection('cssStyle')
             ->addCss('third-partyassets/plugins/fullcalendar/fullcalendar.css', false, false)
             ->addCss('third-party/assets/plugins/assets/css/custom-icon-set.css', false, false)
             ->addCss('assets/css/custom-icon-set.css', false, false)
             ->addCss('css/style.css')
             ->setTargetPath('css/production.css')
             ->setTargetUri('css/production.css')
             ->addFilter(new \Phalcon\Assets\Filters\Cssmin());
    }

.volt File

{{ this.assets.outputCss('cssStyle') }}

When I try to use the following code in the index.php the collection gets recognized. Where is my error??????

$di['assets']
            ->collection('cssStyle')
            ->addCss('stylesheets/vendor/reseter.css')
            ->addCss('stylesheets/vendor/reseter1.css')
            ->addCss('stylesheets/application.css');


2.5k

when and how are you registering the assets?



43.9k

Hi,

First of all, you don't need to register the asset manager in the DI and {{ assets.outputCss(cssStyle) }} is enough.

Then, I think your code is wrong, because second argument should be set to true if your css files are hosted on the same server (see, it works in your second configuration, and not because it is registred in the DI).

More here: https://docs.phalcon.io/en/latest/reference/assets.html#local-remote-resources



959
Accepted
answer

Thanks for the replies and hints for a better code. I found the problem. I overwrote the initialize function in a different controller.