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

Multi Module - default di for view

Hello. I've multi-module app, generated using devtools. In config/services.php we have default DI for application. How can i register Volt engine and some settings (like layout dir) for all modules? Or i need to set view in each Module.php?



43.9k

Hi, here: https://github.com/phalcon/mvc you'll find different multi modules setup (shared views, shared layouts ...).

I used an abstract class like this and then extend from there, it is a bit annoying that the layout path is always prefixed with module path :( you can use a ./../ but cannot seem to enter a absolute path for some reason :(

<?php

namespace CeptPhalcon\Module;

class AbstractModule implements \Phalcon\Mvc\ModuleDefinitionInterface {

    /**
     * We use composer, thanks for the fish
     */
    public function registerAutoloaders() {

    }

    /**
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
     */
    public function registerServices($di) {

        $reflector = new \ReflectionClass(get_class($this));
        $filename = $reflector->getFileName();
        $viewDir = dirname($filename).'/../../views/';
        if (is_dir($viewDir)) {
            //Registering the view component
            $view = $di->get('view');
            if (!$view) {
                $view = new \Phalcon\Mvc\View();
                $view = $di->set('view', $view);
            }
            $view->setViewsDir($viewDir);            
        }
    }
}

And in my bootstrap i already register the view..

$view = new \Phalcon\Mvc\View();
        $view->setLayoutsDir('/../../../views/layout/');        
        $view->setTemplateAfter('main');
        $this->di['view'] = $view;