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.

Registering services with DI using same name in multiple module system

Hi, I'm I working with the mvc/multiple example from GitHub (https://github.com/phalcon/mvc/tree/master/multiple).

In this example, the Dependency Injector (DI) is created in the application level bootstrap file (public/index.php) and then passed to the application where the 2 modules ("frontend" and "backend") are registered.

Each of these modules has a "Module.php" (module level bootstrap). Within each of these bootstraps, I am seeing code to add the "view" to the DI.

1) How does the DI object keep the "view" service separate by module because they are both set using the same identifier string?

2) What if I have a global service, say "globalConfigData" that I want to make available to the 2 modules using the DI - how could I access the global service from within each module and its controllers/views?

Thanks in advance for the help.



44.7k
edited Jan '15

2) What if I have a global service, say "globalConfigData" that I want to make available to the 2 modules using the DI - how could I access the global service from within each module and its controllers/views?

Make a file config/services.php where you put all of the global services. Then called that file from the bootstrap file. Then you can add module specific services and namespace loaders into each module.

Check out my Webird project in the phalcon directory. I spent a bit of time borrowing configuration ideas from other Phalcon frameworks that I found. I wouldn't recommend that you use my project as a whole though because it is too difficult to setup because of the non-phalcon related stuff in it.



2.2k

Thanks, your code is very nicely organized. I am taking a similar approach where I am putting all global services in a config/services.php file.

However, I still have the question about registering with the same service name in different modules. For example, your 'db' service is a global, but in every Module.php you register "view" with a different value (of course) using the "$di->setShared" call. How does the DI maintain two separate "view" services though they are being registered using the same service name (i.e. "view")? What would happen if you were to also register a "view" service at a global level? There would now be > 2 services with the same name, right?

2) What if I have a global service, say "globalConfigData" that I want to make available to the 2 modules using the DI - how could I access the global service from within each module and its controllers/views?

Make a file config/services.php where you put all of the global services. Then called that file from the bootstrap file. Then you can add module specific services and namespace loaders into each module.

Check out my Webird project in the phalcon directory. I spent a bit of time borrowing configuration ideas from other Phalcon frameworks that I found. I wouldn't recommend that you use my project as a whole though because it is too difficult to setup because of the non-phalcon related stuff in it.



1.4k
Accepted
answer

all services are unique to the module.

Eg. If you have a global view and module view, the module view will overwrite the global view. If you have a global view but no module view, the global view will be inherited.

You can set up the view in a global fashion.

add this to the global services.

$di->set('view', function () use (&$config) { $view = new View(); $view->setPartialsDir('partials/'); });

in any module, you can do this to swtich the base view directory to the current module's view directory $view = $di->get('view'); $view->setViewsDir(DIR . '/views/');



2.2k

Thank you for this excellent explanation. I've been able to apply this to correctly separate the global and module-level services.

all services are unique to the module.

Eg. If you have a global view and module view, the module view will overwrite the global view. If you have a global view but no module view, the global view will be inherited.

You can set up the view in a global fashion.

add this to the global services.

$di->set('view', function () use (&$config) { $view = new View(); $view->setPartialsDir('partials/'); });

in any module, you can do this to swtich the base view directory to the current module's view directory $view = $di->get('view'); $view->setViewsDir(DIR . '/views/');



44.7k

How does the DI maintain two separate "view" services though they are being registered using the same service name (i.e. "view")?

@tabalchi I'm only defining a view in the module level and only a single module is run on each execution. The view function is returned from a method defined in the Module base class.

    public static function getViewsDir()
    {
        return self::classNameToDir(get_called_class()) . 'views/';
    }

This static method in the base class gets the module path.

I could have put this view function in the main common services file but it is specific to modules and so I put it in the module logic.