2
votes

I know the multi module application structure of Phalcon, but is it possible to have a nested module structure like following example shows? I want to have a system, where I can hot-plug new sub-modules (for backend, frontend) to the system. Routes, menu entries etc. should get automatically extended when I copy a new module folder into the sub-module folder.

module-backend
    controllers
    models etc.
    sub-modules
        forum
            controllers
            models
            etc.
        news
            controllers
            models
            etc.
        users
            controllers
            models
            etc.
module-frontend
    controllers
    models
    sub-modules
        like backend module structure

Is there a way with events to hot-plug such modules to the system?

2
I think the very least you will have to do is to customise your router with that logic. I thought the proposed approach for multi-module apps is to have all modules at the top level (like backend and frontend in your examples).Ian Bytchek

2 Answers

7
votes

yes you can. the first solution what i can think of is this:

while registering your loader in index.php:

$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
    $config->application->controllersDir,
    $config->application->pluginsDir,
));
$loader->registerPrefixes(
        array(
            "Model_" => $config->application->modelsDir,
        )
);
$loader->registerNamespaces(array(
    'RemoteApi' => $config->application->librariesDir . 'RemoteApi/'
));

$loader->register();

notice registerPrefixes. you can register different prefix for different models like:

$loader->registerPrefixes(
            array(
                "FModel_" => $config->application->forumModels,
                "NModel_" => $config->application->newsModels,
            )
);

you can register prefixes to other things too. I also added this example

$loader->registerNamespaces(array(
    'RemoteApi' => $config->application->librariesDir . 'RemoteApi/'
));

this way you can order your stuff under different namespace's.

1
votes

You can approach my organization structure of modules in the application: https://github.com/oleksandr-torosh/yona-cms

The modules are presented as separate entities: https://github.com/oleksandr-torosh/yona-cms/tree/master/app/modules

  • Index
  • Page
  • Projects
  • Video

Likely for your application you do not need to use the standard multi-modular structure from the box