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

How can a controller in one module access a model in another module?

I'm creating a multi module app, and I want to, for example, list members (members module) on the home page (main module). Using name spaces $this->data['members'] = \App\Users\Models\Members::find(); but I get the error that the requested model can not be found. So how do I let modules interact with each other?



8.1k

You can setup autoloader rightly. Can use namespaces, for example.



20.4k

Thanks for your reply. Can you give me an example of how I would do that?



20.4k

Ah I get it. I just register the namespace for other user module in the Module.php class in main module!



8.1k
Accepted
answer
edited May '14

register the namespace for other user module in the Module.php class in main module!

This is a wrong way... You can use PSR-0. For example. you have main application directory like Forum\


Forum\
           | Apps\
                      |Module1\                   
                      |Module2\                   
                      ***
            |public\
                index.php

Then, you can create autoloader in you ibdex.php PSR-0 corresponding.


/**
 * Register an autoloader
 */
$loader = new \Phalcon\Loader();

$loader->registerNamespaces([
    'Forum' => \dirname(__DIR__) . DS,
]);

$loader->registerDirs([
    \dirname(__DIR__) . DS . 'Apps' . DS . 'Config' . DS
]);

$loader->register();

That's all. You can see PSR-0 standart for more.



20.4k

Thank you Oleg.