We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 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");
    $footerCollection
     ->addJs('assets/js/jquery-3.1.1.js')
     ->addJs(assets/js/bootstrap.js')
     ->setTargetPath($finalJs)
     ->setTargetUri($finalJs)
     ->join(true);
     ->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()
    {
        ...
        $this->assets->collection("libs")->addjs('assets/js/example.js');
    }

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


1.4k

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.