We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

unable to access module model from another module

Hi,

I am getting into Phalcon with a multimodule project. I have a hard time with namespaces and modules interactions. I want to access a module model from another module, frontend module calls a cms module model. Here is how I declared the classes :

// CategoryModel in CmsModule
namespace Application\Cms\Models;
class Category extends \Phalcon\Mvc\Model
{ // ... }
// CategoryController in CmsModule
namespace Application\Cms\Controllers;
use Application\Cms\Models\Category as Category;
class CategoryController extends ControllerBase
{ // Here a new Category() or a Category::class work }

// FrontendController in Frontend module
namespace Application\Frontend\Controllers;
use Application\Cms\Models\Category as Category;
class IndexController extends ControllerBase 
{ // Here a new Category() or a Category::class DO NOT work }

Both modules are declared in apps/config/modules.php and each module/Module.php declares their namespace with registerAutoloaders(DiInterface $di = null)

What am I doing wrong here ?

Thanks in advance for any help.

edited Apr '16

Solved it by registering the Cms module namespace in the Frontend Module.php

class Module implements ModuleDefinitionInterface
{
    public function registerAutoloaders(DiInterface $di = null)
    {
        $loader = new Loader();
        $loader->registerNamespaces(array(
            'Application\Frontend\Controllers' => __DIR__ . '/controllers/',
            'Application\Frontend\Models' => __DIR__ . '/models/',
            'Application\Cms\Controllers' => MODULES_PATH . '/cms/controllers/',
            'Application\Cms\Models' => MODULES_PATH . '/cms/models/',
        ));
        $loader->register();
    }
}

After all this is what I wanted : frontend is the module dependant from a lot of other modules, and the other modules should stay as isolated as possible.

Do you have suggestions on how to improve this ? Should I put a try/catch in this method to check if the required modules are presents ?

You solved it the right way, you have to register needed namespaces in each module.