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

Where to declare and call my css and js

I've recently read up on Assets Management and more specifically Collections and thought that it should be quite simple to set up some css/js and call it easily through multiple web-pages. I've got my collections set up:

$this->assets ->collection('header') ->addCss('css/bootstrap.min.css');

$this->assets ->collection('footer') ->addJs('js/jquery.min.js') ->addJs('js/bootstrap.min.js');

I initially declared these in controllers/IndexController then called them using volt in view/index.volt eg:

<head> <title>Phalcon PHP Framework</title> {{ assets.outputCss('header') }} </head>

But I've found this does not reach any other views and I get the error The collection does not exist in the manager.

A little help on understanding to best set this up and what I'm doing wrong would be greatly appreciated. Thank you!



43.9k
Accepted
answer
edited May '14

Hi,

If you want to get all your assets loaded for every controllers end their respective actions, you can register them (as you have done) in ControllerBase.php in initialize() function.

Then, whenever you need a special asset:

  • you can declare them per controller in initialize function of that controller. asset will then be available for every actions in that controller
  • or declare them per action ...


4.7k

Hi le51, thanks so much for your reply.

Took your advice and moved my collections to /app/controllers/ControllerBase.php to the initialize() function. Created a TestController that extends ControllerBase and it works perfectly! Great, thanks for the heads up on assets per controller via initialize() or per action.



43.9k

you're welcome.