Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



7.1k

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



10.2k

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



10.2k

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



7.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.



10.2k

Thank you Oleg.