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?



7.4k

Multiple inheritance in Volt probably what you are looking for



6.7k
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.



7.4k
Accepted
answer

Sorry, I missed modules

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

    <?php
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();

        $loader->registerNamespaces(array(
            'Evc\Backend\Controllers' => realpath(__DIR__ . '/controllers/')
        ));

        $loader->register();
    }

    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());

        $dispatcher->setEventsManager($eventManager);

        $dispatcher->setDefaultNamespace("Evc\\Backend\\Controllers\\");

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

        $di->set('assets', function() use ($config) {
            $assets = new \Phalcon\Assets\Manager();
            $assets
                ->collection('footerJS')
                ->addJs('//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js', false)
                ->addJs('//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js', 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)
            ;
            $assets
                ->collection('headerCss')
                ->addCss('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css', false, false)
                ->addCss('https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css', false, false)
                ->addCss('https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css', 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



6.7k
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.