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 folder structure

Hello,

I am rebuilding an old multi module application with Phalcon, but I can't figure out the Routes.

This is how I have the folder structure: https://i.imgur.com/jhovFSI.jpg

I will have 3 modules: Rest, Frontend, Backend. I would like to have the "Models" shared to all of the modules.

I can't figure out the "Default" / "ClientJhon" folder structure inside "Models" or "Controllers".

Depending on the user login data or the Rest call authorization, a client might have a different structure, so I should load his "Controllers" / "Models".

Can somebody help me out with the Routes? Or give me something similar.

Thanks, Azazeal



7.6k
edited Mar '17

When you create new Project via phalcon-devtools (the new one, not some old!) you will have very nice structure

--app
----common
----config
----modules

you can put models in app/common/models declare it in app/config/loader.php

like:

$loader->registerNamespaces([
    'Application\Models' => APP_PATH . '/common/models/',
    'Application'        => APP_PATH . '/common/library/',
]);

and in app/config/routes.php you can have


$router = $di->getRouter();

foreach ($application->getModules() as $key => $module) {
    $moduleRoute = APP_PATH . "/modules/$key/routes.php";
    if (file_exists($moduleRoute)) {
        require $moduleRoute;
    }
}

that i have and it work perfectly

edited Mar '17

Hello,

Thank you for your reply! But it dosen't work. My url are like this:

  • :module/:submodule/:controller/:action/:params

Example:

  • api/realestate/morgages/edit (module->api,submodule->realestate,controller->morgages,action->edit)
  • realestate/morgages/edit (module->frontend,submodule->realestate,-controller>morgages,action->edit)

  • api/fincial/billing/generate (module->api,submodule->fincial,controller->billing,action->edit)
  • financial/billing/generate (module->frontend,submodule->fincial,controller->billing,action->edit)

And then I have the second problem I can't figure out. Dependign on the user account or API key I should load custom controller. For example inside my realestate controller folder, I might have a file "realestate/controllers/client1/MorgagesController.php" that I should load insted of "realestate/controllers/MorgagesController.php"

When you create new Project via phalcon-devtools (the new one, not some old!) you will have very nice structure

--app
----common
----config
----modules

you can put models in app/common/models declare it in app/config/loader.php

like:

$loader->registerNamespaces([
   'Application\Models' => APP_PATH . '/common/models/',
   'Application'        => APP_PATH . '/common/library/',
]);

and in app/config/routes.php you can have


$router = $di->getRouter();

foreach ($application->getModules() as $key => $module) {
   $moduleRoute = APP_PATH . "/modules/$key/routes.php";
   if (file_exists($moduleRoute)) {
       require $moduleRoute;
   }
}

that i have and it work perfectly



43.9k

Hi,

:module/:submodule/:controller/:action/:params

is this what you want to achieve, or do you already get a working solution for that ?

SOme thoughts from what I understand from your problematic:

I would like to have the "Models" shared to all of the modules

put them in an app/common/models directory

Dependign on the user account or API key I should load custom controlle

for per user access on controllers and actions, use phalcon acl to control that and use a per user specific $dispatcher->forward after user authentication