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 not working

Hy Friends!

I am working on a bigger project and would like to use a multi module application to archive my goals. But I have a problem with routing. I always get following error for url (https://dev.local/backend/index/index):

Application\Modules\Frontend\Controllers\BackendController handler class cannot be loaded

My Bootstrap File:

<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Collection\Manager;
use Phalcon\Mvc\Router;

ini_set("display_errors", "on");
error_reporting(E_ALL);

$di = new FactoryDefault();

/**
 * Router
 */
$di->set("router", function () {
    $router = new Router(true);
    $router->setDefaultModule('frontend');
    return $router;
});

/**
 * URI
 */
$di->set("url", function() {
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri("/");
    return $url;
});

/**
 * MongoDB
 */
$di->set("mongo", function () {
    $mongo = new MongoClient('mongodb://localhost:27017');
    return $mongo->selectDB('iowling');
});

/**
 * MongoDB Collection Manager
 */
$di->set("collectionManager", function() {
    $manager = new Manager();
    return $manager;
});

try {
    $application = new \Phalcon\Mvc\Application($di);
    $application->registerModules(array(
        'frontend' => array(
            'className' => \Application\Modules\Frontend\Module::class,
            'path' => __DIR__ . '/../modules/frontend/Module.php'
        ),
        'backend' => array(
            'className' => \Application\Modules\Backend\Module::class,
            'path' => __DIR__ . '/../modules/backend/Module.php'
        )
    ));

    $response = $application->handle();
    $response->send();

} catch (Exception $e) {
    echo $e->getMessage();
}

My backend Module:

<?php
namespace Application\Modules\Backend {

    use Phalcon\Loader;
    use Phalcon\Mvc\Dispatcher;
    use Phalcon\Mvc\ModuleDefinitionInterface;

    class Module implements ModuleDefinitionInterface
    {
        public function registerAutoloaders(\Phalcon\DiInterface $dependencyInjector = null)
        {
            $loader = new Loader();
            $loader->registerNamespaces(array(
                'Application\Modules\Backend\Controllers' => __DIR__ . '/../backend/controllers/',
                'Application\Modules\Backend\Collections' => __DIR__ . '/../backend/collections'
            ));
            $loader->register();
        }

        public function registerServices(\Phalcon\DiInterface $dependencyInjector)
        {
            $dependencyInjector->set("dispatcher", function () {
                $dispatcher = new Dispatcher();
                $dispatcher->setDefaultNamespace(
                    'Application\Modules\Backend\Controllers'
                );
                return $dispatcher;
            });
        }

    }
}

My Frontend Module

<?php
namespace Application\Modules\Frontend {

    use Phalcon\Loader;
    use Phalcon\Mvc\Dispatcher;
    use Phalcon\Mvc\ModuleDefinitionInterface;
    use Phalcon\Mvc\View;

    class Module implements ModuleDefinitionInterface
    {

        public function registerAutoloaders(\Phalcon\DiInterface $dependencyInjector = null)
        {
            $loader = new Loader();
            $loader->registerNamespaces(array(
                'Application\Modules\Frontend\Controllers' => __DIR__ . '/../frontend/controllers/',
                'Application\Modules\Frontend\Collections' => __DIR__ . '/../frontend/collections/'
            ));
            $loader->register();
        }

        public function registerServices(\Phalcon\DiInterface $dependencyInjector)
        {
            $dependencyInjector->set("dispatcher", function () {
                $dispatcher = new Dispatcher();
                $dispatcher->setDefaultNamespace(
                    'Application\Modules\Frontend\Controllers'
                );
                return $dispatcher;
            });

            $dependencyInjector->set("view", function () {
                $view = new View();
                $view->setViewsDir(__DIR__ . '/views/');
                $view->registerEngines(array(
                    '.volt' => function($view, $dependencyInjector) {
                        $volt = new View\Engine\Volt($view, $dependencyInjector);
                        $volt->setOptions(array(
                            "compiledPath" => __DIR__ . '/../../cache/',
                            'compileAlways' => true
                        ));
                        return $volt;
                    }
                ));
                return $view;
            });
        }
    }
}

Did you guys have any idea what I've done wrong? I would really appriciate it.

Thx!



145.0k
Accepted
answer
edited Mar '17

By default router have only routes for /controller/action and /controller/action/params. You just need to add:

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


3.5k

Thx! Now it works fine!

By default router have only routes for /controller/action and /controller/action/params. You just need to add:

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