Use Module A Model in Module B Controller

Hello.

How can I use each module models into another one? I use namespace to handle this but failed.

Here is my code:

in module A Model :

 namespace Modules\People_Zone\Models;

 class People extends \Phalcon\Mvc\Model {
 ...

 }

And in module B Controller:

use Modules\People_Zone\Models\People as People;

and in controller's action:

$peoples =  People::find();

but my model was not found, then I register my module model's namespace in the main loader object ( in index.php ) and it's working, so we should do this for every modules ?

There is a way to toad every namespace you need, I had to reproduce a structure I know well, and so I messed up a little with config and loader to force loading Module.php before dispatch.

So basically I have an /Config/application.config.php where I register every module, and tell if module will be "shared".

return array(
    'module'        => array(
        'Core'          => array(
            'className'     => 'Core\Module',
            'path'          => __DIR__ . '/../Module/Core/Module.php',
            'shared'        => true,
        ),
        'Test'          => array(
            'className'     => 'Test\Module',
            'path'          => __DIR__ . '/../Module/Test/Module.php',
        ),
    ),
);

In my bootloader I load module that way :

$di = new FactoryDefault();

// Registering vendors namespaces
$loader = new Loader();
$loader->registerNamespaces(require __DIR__ . "/Vendor/register_namespace.php", true);
$loader->register();

// Import Application Configuration
$appConfig = new Config(require __DIR__ . '/Config/application.config.php');
// Registering Application config as a service
$di->set('appConfig', $appConfig);

// Import Global and Local configuration
$globalConfig = new Config(require __DIR__ . '/Config/global.config.php');
$globalConfig->merge(new Config(require __DIR__ . '/Config/global.config.php'));
$di->set('config', $globalConfig);

foreach ($appConfig['module'] as $module) {
    $dir = preg_replace("/\/Module.php$/", "", $module['path']);
    // Register Namespace for Shared Module (will not be called by dispatch)
    if (array_key_exists('shared', $module) && $module['shared']) {
        $namespace = preg_replace('/\\\Module$/', "", $module['className']);
        $loader->registerNamespaces(array($namespace => $dir), true);
        // Loading Module class bootloader Reflection Instance
        $reflexion = new ReflectionClass($module['className']);
        $instance = $reflexion->newInstanceWithoutConstructor();
        // Execute every 'register' methods
        foreach ($reflexion->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
            if(preg_match('/^register/', $method->name)) {
                $instance->{$method->name}($di);
            }
        }
    }
}


10.4k

@SmasherHell : Thank you, you write a new framework for yourself :D

but what is this "shared" attribute ?

I think its could be easiest to shared modules classes, in Kohana 3 you should just to register module name in the bootstrap file and sewt the route for module thats all, then your modules classes are accessible from anywhere.

The shared attribute catched in bootloader, when shared is true, the Module.php in module is instanciated and every method begining with 'register' in Module class is executed. The registerAutoloader method register namespaces and class, so the shared module is accessible from anywhere