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

Adding a service layer to a multi module application so services are shared across modules

Hi,

I've produce a multi module application but I'm wondering what is the best approach to adding a service layer where services can be accessed across modules.

I've followed this example https://github.com/phalcon/mvc/tree/master/multiple-service-layer-model

I've created a module which contains the shared models and also created a component called ServiceLoader like this one( but not abstract) https://github.com/phalcon/mvc/blob/master/multiple-service-layer-model/apps/models/services/Services.php

Now should I add this class to the application DI and for each service that I want to be shared should I create a corresponding entry like so:


    <?php

    $di->setShared(
          'serviceLoader',
          function () {
             return new ServiceLoader();
              );
          }
      );

      $di->setShared(
          'userService',
          function () {
             return $this->get('serviceLoader')->getService('UserService');
              );
          }
      );

Then in my controllers I should be able to use something along the lines of:

userService::getUser($userId);

Many thanks

Just register services which you want across module outside of module.