Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Share assets/collections across multiple modules is that possible?

Hello. I have js and css, which I know will be used across all modules e.g. boostrap.min.css. Do I have to repeat the same code that adds css/js in ControllerBase for each module? I have a feeling there should be more beautiful way. Something similar to shared layouts. Any ideas?


Multiple inheritance in Volt probably what you are looking for

edited Dec '15

Hi, valVk. Not sure how multiple inheritance will help me. What I want to achieve is set application-wide assets one time. For single module application I do it from BaseController:

class BaseController extends \Phalcon\Mvc\Controller {
public function initialize() {
$this -> assets 
              -> collection('style')            
              -> addCss('third-party/css/font-awesome.min.css',true,false)
              -> addCss('third-party/css/bootstrap.min.css',true,false)              
              -> addCss('css/style.css')
              -> setTargetPath('css/production.css')
              -> setTargetUri('css/production.css')
              -> join(TRUE)
              -> addFilter(new \Phalcon\Assets\Filters\Cssmin());

$this -> assets
              -> collection('js')
              -> addJs('third-party/js/jquery-2.1.4.min.js',true,true)
              -> addJs('third-party/js/bootstrap.min.js',true,true)
              -> setTargetPath('js/production.js')
              -> setTargetUri('js/production.js')
              -> join(TRUE)
              -> addFilter(new \Phalcon\Assets\Filters\Jsmin());

All examples I've seen in documentation suggested to add collection from BaseController. The problem is that BaseController is unique for every module, so I need to initialize collection from code that shared among modules to avoid repetition, but when I try to do so from config or layout, I get an error.


Sorry, I missed modules

You can add assets in service I did something like that at module when registered it

namespace Evc\Backend;

use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Logger;
use Phalcon\Logger\Adapter\File as FileLogger;
use Phalcon\Events\Manager as EventsManager;

class Module implements ModuleDefinitionInterface

    public function registerAutoloaders( \Phalcon\DiInterface $dependencyInjector = null )
        $loader = new \Phalcon\Loader();

            'Evc\Backend\Controllers' => realpath(__DIR__ . '/controllers/')


    public function registerServices( \Phalcon\DiInterface $di )
        $config = include __DIR__."/../../shared/config/".ENVIRONMENT.".config.php";
        $dispatcher = $di->get('dispatcher');

        $eventManager = $di->getShared('eventsManager');

        //$eventManager->attach('dispatch:beforeExecuteRoute', new Gate());



        $di->get('view')->setViewsDir(realpath(__DIR__ . '/views/'));

        $di->set('assets', function() use ($config) {
            $assets = new \Phalcon\Assets\Manager();
                ->addJs('//', false)
                ->addJs('//', false)
                ->addJs('p/resources/backend/plugins/fastclick/fastclick.min.js', true)
                ->addJs('p/resources/backend/dist/js/app.min.js', true)
                ->addJs('p/resources/backend/plugins/slimScroll/jquery.slimscroll.min.js', true)
                ->addCss('', false, false)
                ->addCss('', false, false)
                ->addCss('', false, false)
                ->addCss('p/resources/backend/dist/css/AdminLTE.css', true)
                ->addCss('p/resources/backend/dist/css/skins/skin-blue.min.css', true)
                //TODO use minified tools
//              ->addCss($config->application->backendAssets.'/plugins/jvectormap/jquery-jvectormap-1.2.2.css', true)
//              ->setTargetPath($config->application->documentRoot.'/assets/css/css.minified.backend.css')
//              ->setTargetUri('/assets/css/css.minified.backend.css')
//              ->join(true)
//              ->addFilter(new \Phalcon\Assets\Filters\Cssmin())

            return $assets;

as you can see you can easily add it to sevices and use it in volt

edited Dec '15

Hi, valVk. That's just what I've needed: add assets to services on app config level. Now I don't need to repeat this code for each module. Thank you.