Ildar
Ildar

Reputation: 796

How to extend moduleManager in Zend Framework 2?

I want to extend standard Zend\ModuleManager\ModuleManager, is it possible? For example I want to load modules list from database and I want to add some methods for working with modules.

If I set factory to serviceManager:

'service_manager' => array(
    'factories' => array(
        'moduleManager' => 'Path/To/My/ModuleManager',
    ),
),

There is error "A service by the name or alias "modulemanager" already exists and cannot be overridden, please use an alternate name"

Upvotes: 1

Views: 660

Answers (1)

Stoyan Dimov
Stoyan Dimov

Reputation: 5529

The module manager in Zend Framework 2 is created via a service factory class. ModuleManager is mapping to Zend\Mvc\Service\ModuleManagerFactory. I advice you to have a look on this ModuleManagerFactory and see what object are injected in the module manager on createService().

If you want to extend and use your own module manager you must create a class that extends ModuleManager but also create a service manager factory that overwrites the Zend\Mvc\Service\ModuleManagerFactory. You are on the right way with the following code but it is important to put this code in the /config/application.config.php file because this is the config file that Zend\Mvc uses to create the main services.

// config/application.config.php
'service_manager' => array(
    'factories' => array(
        'ModuleManager' => 'Path/To/My/ModuleManagerFactory', // <-- Path to MM factory
    ),
),

The link below will give you good information about what default services are run with \Zend\Mvc and how and where this is happening:

https://zf2.readthedocs.org/en/latest/modules/zend.mvc.services.html

Hope this helps, feedback will be appreciated :)

Stoyan

Upvotes: 2

Related Questions