We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Load assets to all views

So, I have a bunch of controllers and views in a project. And I want to set my assets (CSS and JavaScript) just once instead of replicate it to every controller that have views. How can I do that?

For each new controller that I have I need to put this code to get the assets loaded:

    private function loadAssets($isProduction)


             ->addCss(csspath.'bootstrap.min.css', true)
             ->addCss(csspath.'master/styles.dashboard.css', true);

             ->addJs(jspath.'jquery.min.js', true)
             ->addJs(jspath.'master/module.animations.js', true)
             ->addJs(jspath.'master/module.actions.js', true);

Where can I set this function to load automatically into all my other controllers/views? I've tried IndexController and ControllerBase, but they didn't worked.

edited Jun '15

You can create a base controller like this:

class ControllerBase extends Controller
    private $isJsonResponse = false;

    public function initialize()
        //Javascripts in the header
            ->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)

        //Css in the header

And than you need to extend this ControllerBase from your controllers

Thanks, it worked!