Set module dependencies in Zend 2

One of the core concepts behind Zend 2 is the use of 'modules' as a method of grouping functionality (e.g. a contact module, or forum module). The goal of this structure is that you should be able to 'drag-and-drop' modules into different applications without the coupled dependencies of unrelated functionality and code. In some cases however, modules will have dependencies of third party vendor libraries, or on other modules. This article shows you how to define this dependencies.

Vendor library dependencies

Vendor dependencies can be set within a json composer file that sits in the root of your module folder. As an example you would add the following to set Doctrine ORM as a dependency for your module:

"require": {
    "doctrine/doctrine-orm-module": "0.7.*",

Running composer will then update the vendor folder with any dependencies.

Other module dependencies

If your module is dependent on other modules you use the DependencyIndicatorInterface in your module.php file like so:

use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
class Module implements DependencyIndicatorInterface
    public function getModuleDependencies()
        return array('OtherModule');    

If the dependent module isn't loaded before the subject module, a dependent error will occur. Modules are loaded in the /config/application.config.php file, note that the dependent module must be listed before the subject module otherwise you'll still receive the same error.

Sign Up

NEXT: Connected Multiselect Boxes

The following code snippet is a quick and easy way to selectively create a list from options in a secondary multiselect box. As it's multiselect you'll be able to move more than one option to the other list at the same time.

comments powered by Disqus
Sign Up

Popular Tags


Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.