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

Add js to collection and minify from another controller

Hi! Newbie here! Can't find answer in my head.

Let's say, I have following code in ControllerBase.php:

    $finalJs = "assets/js/scripts.min.js";
    $footerCollection = $this->assets->collection("libs");
     ->addFilter(new \Phalcon\Assets\Filters\Jsmin());

And how add to this collection for minify from another controller, let's name it ExampleController.php:

    public indexAction()

Thanks for advice!

Well... your code looks fine for me. I have similar snippets and it's working for me.

If it's not working, I'd check the setTargetPath and setTargetUri. The first one should point to the local file path, the second to the web path.

For eg:

  • www root: /var/www/site.com/public
  • target path: /var/www/site.com/public/js/scripts.min.js
  • target uri: /js/scripts.min.js


Yes, it outputs scripts:

assets/js/scripts.min.js assets/js/example.js

But my goal is to merge "scripts.min.js" and "example.js" with Jsmin().

If I add "example.js" to collection in another controller it won't merge just outputs two scripts :)

Be carefull

$this->assets->collection("libs")->addJs('assets/js/example.js'); //is addJs

other way use a plugin to excecue just before print layout

I understand it correctly ? You are trying to merge collection and js file ? Then i think it's not possible.