views:

134

answers:

1

How do you handle dependencies between modules in Zend Framework to create reusable, drop-in modules?

E.g. I have Newsletter module, which allows users to subscribe, providing e-mail address.

Next, I plan to add Blog module, which allows to subscribe to posts by e-mail (it obviously duplicates some functionality of the newsletter, but the e-mails addresses are stored in User model). Next one is the Forum module, with the same subscribe to post functionality.

But I want to have ability to use these modules independent to each one, i.e. application with newsletter alone, newsletter with blog, comibnation two or three modules at once.

This is pretty common, e.g. the same story with search feature. I want to have search module, with options to search in all data, blog data or forum data if available.

Is there any design pattern for this?
Do I have to add some moduleExists($moduleMame), or provide some interface or abstract classes, some base controller pattern, similar for each module?

A: 

I would probably use some kind of dependency injection container pattern to register your modules with the container on application initialization. Then, you can load models and be sure they have all the appropriate dependencies.

For the subscription feature, I would create a common subscription model in a more abstracted library that can be used by each module through a common API where needed.

In the case of the search feature knowing which other modules to search: When you load the search model through the container, the container can inform the search model of the registered modules to search.

It may be easier to hack it up and store registered modules in the registry to be accessed by common modules such as search, but using the DI Container will prove to be a good pattern to solve many similar problems.

delsurf