Phalcon Module definition / understanding


i have a question regarding the definition of the Modules in the Phalcon Application.

I tried to create a multi module application but it is always the case that just the module is loaded which i defined for the default routing and dispatcher.

The other Modules are not loaded. I tried and debugged many cases but it still won't work until i define a route that leads to the module and the controller.

I would like to describe the expected behaviour for me.

I want to split the Application into multiple Modules.

I need a "frontend" Module for the web application and a "database" module that contains all models / classes that communicate with the database. The Purpose of this splitted modules is that i just want to require the database module in order to work with the stored data. So i can use a Frontend Module for the Web and if i want to build an API i just create a new module "api" that requires the "database" module without requiring the frontend module . This way i can use parts of the Application in multiple was without needing to load every Module.

My problem is that i cannot access the models because the database module is not loaded, probably because no route is leading to this module.

Is there a way to trigger the load of a module manually?

Thanks in advance

edited Nov '16

You can try sometning like:

$modules = [
    'database' => [
        'className' => DatabaseModule::class,
        'path'      => MODULES_PATH . '/database/Module.php',
    'frontend' => [
        'className' => FrontendModule::class,
        'path'      => MODULES_PATH . '/frontend/Module.php',
        'router'    => MODULES_PATH . '/frontend/config/router.php',
    'oauth' => [
        'className' => oAuthModule::class,
        'path'      => MODULES_PATH . '/oauth/Module.php',
        'router'    => MODULES_PATH . '/oauth/config/router.php',
    'oauth' => [
        'className' => BackendModule::class,
        'path'      => MODULES_PATH . '/backend/Module.php',
        'router'    => MODULES_PATH . '/backend/config/router.php',
    'cli' => [
        'className' => CliModule::class,
        'path'      => MODULES_PATH . '/cli/Module.php',

$mvcModules = [];
foreach ($modules as $name => $module) {
    // Here $di is DiInterface
    $mvcModules[$name] = function () use ($module, $di) {
        $moduleClass = $module['className'];
        if (!class_exists($moduleClass)) {
            include_once $module['path'];

        $moduleBootstrap = new $moduleClass($di);


        return $moduleBootstrap;

    $di->setShared($module['className'], $mvcModules[$name]);

/** @var \Phalcon\Cli\Console|\Phalcon\Mvc\Application $application */
if ($application instanceof \Phalcon\Cli\Console) {
} else {

Of course you need to register routes. For example each module can provide RouterGroup.

edited Nov '16

Thanks Sergej,

this is exactly what i mean. I wrote a very similiar workaround to get my modules loaded. Sorry that i didn't attached a Code example.

But wouldn't it be great to have a native implementation of this Behaviour?

Something like an additional Parameter like the following example:

    'base' => [
        'className' => 'Application\Base\Module',
        'path' => __DIR__ . '/apps/base/Module.php',
        'autoload' => true // or 'on_startup' 

This looks like a New Feature Request. Could you please open an issue at phalcon repo?

edited Nov '16

Done :)

Hope i created the Ticket with enough information.