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

Share Library or Plugins to use in Multiple Module

I have file structure like this

MVC

I wonder how to share library or plugins from outside module to inside module. eg. call Security.php from privateModuleA

the goal is dispatch Security ACL plugins to EventManager in Multiple Module

I try to add this code to app/modules/privateModuleA/Module.php but It's not working

<?php

namespace Modules\privateModuleA;

use Phalcon\Loader,
    Phalcon\Mvc\Dispatcher,
    Phalcon\Mvc\View,
    Phalcon\Mvc\ModuleDefinitionInterface;

use \Base\Plugins\Security as Security;

class Module implements ModuleDefinitionInterface
{

    /**
     * Register a specific autoloader for the module
     */
    public function registerAutoloaders()
    {

        $loader = new Loader();

        $loader->registerNamespaces(
            array(
                'Modules\privateModuleA\Controllers' => __DIR__ . '/controllers/',
                'Modules\privateModuleA\Models'      => __DIR__ . '/models/',
                'Base\Plugins\Security' => __DIR__ . '/../../plugins/Security.php',
            )
        );

        $loader->registerClasses(
            array(
                "Base\Plugins\Security" => "../../plugins/Security.php",
            )
        );

        /**
         * We're a registering a set of directories taken from the configuration file
         */
        $loader->registerDirs(
            array(
                __DIR__ . '/../../plugins/',
            )
        );

        $loader->register();
    }

    /**
     * Register specific services for the module
     */
    public function registerServices($di)
    {
        // include __DIR__ . "/../../plugins/Security.php";
        /**
         * We register the events manager
         */
        $di->set('dispatcher', function() use ($di) {

            $eventsManager = $di->getShared('eventsManager');

            $security = new Security($di);
            $eventsManager->attach('dispatch', $security);

            $dispatcher = new Dispatcher();
            $dispatcher->setEventsManager($eventsManager);

            return $dispatcher;
        }, true);

        //Registering the view component
        $di->set('view', function() {
            $view = new View();
            $view->setViewsDir(__DIR__ . '/views/');
            return $view;
        });
    }

}

please help.

One year later I also have this problem. Did You find solution?

I think If you loaded the security class in the boostrap file so that you can use it in all inside modules. You do not need to re-load it again.

If you load the security class in each module, maybe you should check it's path, namespaces, v.v. to make sure it loaded before using.