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 routing without default module

Hi guys,

Is it possible to define multimodule routing without default module (setDefaultModule) ? I have same error when trying without setDefaultModule like in this thread:

https://forum.phalcon.io/discussion/2736/service-view-was-not-found-in-the-dependency-injection-container

<?php

use Phalcon\Mvc\Router;

$router = new Router(false);

$router->add(
    '/:module/:controller/:action/:params',
    array(
        'module'     => 1,
        'controller' => 2,
        'action'     => 3,
        'params'     => 4
    )
);

Service 'view' was not found in the dependency injection container



85.5k
edited Oct '15

I probably can't solve your problem. However i dont have setDefaultModule in my routes.

$this->add('/', array(
            'module' => 'home',
            'controller' => 'index',
            'action' => 'index',
            'namespace' => 'MY_NS\Home\Controllers\\'
        ))->setName('default');

but my guess is that you problem comes from Module.php

( its a pure guess I am not master of those views / layouts I had many issues with them )

//edit you define View in your DI for this module right ? Show us Module.php "registerServices" function

edited Oct '15

Thanks for reply @Izo,

It seems that in your example you need to define routes manually each time. Not the case I'm looking for.

In my Module.php I load services dynamically:

public function registerServices(DiInterface $di)
    {
        // list of services that should be defined
        $services = [
            '\Modules\Tools\Services\ConfigService',
            '\Modules\Tools\Services\DispatcherService',
            '\Modules\Common\Services\ModelsDataService',
            '\Modules\Common\Services\SessionService',
            '\Modules\Common\Services\UrlService',
            '\Modules\Common\Services\ViewService',
        ];

        // iterate over services and define/set them
        foreach($services as $serviceNameSpace){

            /** @var \Modules\Common\Services\AbstractService $service */
            $service = new $serviceNameSpace;
            $service->define();
        }
    }

Views service contains:

        $this->_calls = [

            // set views directory
            [
                'method'    => 'setViewsDir',
                'arguments' => [
                    [
                        'type'  => 'parameter',
                        'value' => $this->_di->get('config')->application->dirs->views
                    ]
                ]
            ],

            // set template engine
            [
                'method' => 'registerEngines',
                'arguments' => [
                    [
                        'type' => 'parameter',
                        'value' => [
                            '.volt' => function(){

                                $volt = new \Phalcon\Mvc\View\Engine\Volt($this->resolve(), $this->_di);

                                $volt->setOptions([
                                    'compiledPath'      => $this->_di->get('config')->application->dirs->cache,
                                    'compiledSeparator' => '_'
                                ]);

                                return $volt;
                            },
                            '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
                        ]
                    ]
                ]
            ]
        ];

Router service contains:

protected $_calls = [
        [
            'method'    => 'setDefaultModule',
            'arguments' => [
                [
                    'type'  => 'parameter',
                    'value' => 'tools'
                ]
            ]
        ],

        [
            'method' => 'add',
            'arguments' => [
                [
                    'type' => 'parameter',
                    'value' => '/:module/:controller/:action/:params'
                ],

                [
                    'type' => 'parameter',
                    'value' => [
                        'module'     => 1,
                        'controller' => 2,
                        'action'     => 3,
                        'params'     => 4
                    ]
                ]
            ]
        ],

        [
            'method'    => 'removeExtraSlashes',
            'arguments' => [
                [
                    'type'  => 'parameter',
                    'value' => true
                ]
            ]
        ]
    ];

$this->_calls / protected $_calls - is an array that will be passed as setter injection: https://docs.phalcon.io/en/latest/reference/di.html#setter-injection



85.5k
edited Oct '15

what's the name of the module here and the exact url ? ( where you have issues )

//edit

also go to this module Module.php, inside registerServices function type :

echo "yeaa";exit;

just to be sure, I had this issue recentyly and my router wansnt working correctly ( so my module wasnt initiated at all ).

also i had issues with namespacing ( capital and small letters ).

in those cases I had the same error

@Izo,

Module works. And routing also works with setDefaultModule The question was if its possible to define dynamic routing without setDefaultModule like in head post example.



105

@ph55

Hi Alex, I am trying to start a large project with multi-module(publisher/advertiser/admin/common/apiv1/apiv2 etc), multi-database(pubdb/advdb etc), could you please share your project folder structure with code? Thanks in advance.



85.5k

https://www.npmjs.com/package/generator-phalcon

when generated it has few errors, like warrning etc... you have to put fre $di = \Phalcon\DiInterface in few function params and thats pretty much it. It works works with multi module like a charm