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

Assets Management example with layout file?

Hi, I'm trying to integrate Assets Management into my application. I'm running into errors and can't seem to find any examples of how to use Assets Management within a layout.

What I have right now is this in my layout.phtml:

    <!DOCTYPE html>
    <html lang="en" data-ng-app="MyApp">
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">

            <!-- Foundation CSS -->
            <link href="/lib/foundation/css/foundation.min.css" rel="stylesheet"/>

            <!-- JQuery UI CSS -->
            <link href="/lib/jquery-ui/jquery-ui.min.css" rel="stylesheet"/>

            <style type="text/css">
                [ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
                    display: none !important;

            <!-- LESS CSS -->
            <link rel="stylesheet/less" type="text/css" href="/css/style.less" />
            <script type="text/javascript" src="/lib/less/dist/less.min.js"></script>

            <?php $this->assets->ouputJs(); ?>


and in my ControllerBase, I have this:

    public function beforeExecuteRoute(Dispatcher $dispatcher)

I'm getting errors of call to undefined method of outputJs() and join(). I'm guessing I'm missing a use or am I doing this completely wrong?


i noticed something fishy there aswell, i am nto sure what is the "real" fix, but this is how i use my assests

                ->addJs('js' . DS . 'vendor.js')
                ->addJs('js' . DS . 'scripts' . DS . 'Utils.js');

and then in my layout i have



How do you define $assetManager?


$this->di->set('assets', function(){

            $assetManager = new \Phalcon\Assets\Manager();

                ->addCss('css' . DS . 'vendor.css')
                ->addCss('css' . DS . 'bootstrapValidator.min.css');

                ->addJs('js' . DS . 'vendor.js')
                ->addJs('js' . DS . 'scripts' . DS . 'Utils.js')
                ->addJs('js' . DS . 'bootstrapValidator.min.js')
                ->addJs('js' . DS . 'script.js');

            return $assetManager;
}, true);


Grr... Still getting errors. Added in my services.php:

        $assetManager = new \Phalcon\Assets\Manager();
        return $assetManager;

..and in my controller:

    public function initialize() {
        $assetManager = $this->getDi()->getShared('assets');



...and in my layout:

    <?php $this->assets->ouputJs('js'); ?>

I'm getting the error: PHP Fatal error: Call to undefined method Phalcon\Assets\Manager::ouputJs() in /opt/www/html/zipline/app/views/layouts/layout.phtml on line 25.

If I print_r $assetManager, I see the js files have been added. Seems to be a problem with the display.



did you set "assets" service as shared ?


It's set up the same way the other services are set up and I access them as above... cache, other custom services, etc.


jsut try with $di->setShared('assets', function....);