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

Models in other Module

Hello, How can i use a model in other module ?



2.9k
Accepted
answer
edited Oct '14

That depends I have a multi module setup with a Core module where the models are all in. In the module config files I point to that folder for the models. But this means all models are in one place.

modelsDir = ../apps/core/models/

Inside the Module bootstrapper I register the models namespace.


class Module implements ModuleDefinitionInterface
{

    /**
     * Registers the module auto-loader
     */
    public function registerAutoloaders()
    {

        $loader = new Loader();

        $loader->registerNamespaces(array(
            'Shinzou\Account\Controllers' => __DIR__ . '/controllers/',
            'Shinzou\Core\Models' => __DIR__ . '/../core/models/',
            'Shinzou\Core\Controllers' => __DIR__ . '/../core/controllers/',
            'Shinzou\Account\Forms' => __DIR__. '/forms',
            'Shinzou\Core\Library\Mail' => __DIR__ . '/../core/library/Mail/',
        ));

        $loader->register();
    }

Hope this helps...

Regards,

Alex



8.1k

Smal note. If you read about PSR-0, you can make your code shorter :)


$app_root = ''; // directory root of you namespace
$loader->registerNamespaces(array(
            'Shinzou' => $app_root
        ));