Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Check Module Dependency

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on February 1, 2013

zf2-zendframework2 Zend Framework 2.0.7 and 2.1 were released! They come with new features and more improvements. One of the improvements is ability to check other module already loaded before. It provide Zend\ModuleManager\Feature\DependencyIndicatorInterface.
The sample code is like the following :

namespace Mod2;

use Zend\ModuleManager\Feature\DependencyIndicatorInterface;

class Module implements DependencyIndicatorInterface
{   
    public function getModuleDependencies()
    {
        return array('Mod1');   
    }
    
    public function getConfig() { /* common code here */ }
    public function getAutoloaderConfig() { /* common code here */ }
}

If Mod1 is not loaded before Mod2, it will show the exception : Module “Mod2” depends on module “Mod1”

References :
1. https://github.com/zendframework/zf2/issues/3427
2. https://github.com/zendframework/zf2/pull/3443

Advertisements

9 Responses

Subscribe to comments with RSS.

  1. Guilhem said, on February 2, 2013 at 12:03 am

    That’s a good news, thanks 🙂

  2. Mayckon said, on March 9, 2013 at 12:30 am

    Can you give me a example of a situation use this? I’m new with ZF2, thx.

    • samsonasik said, on March 9, 2013 at 12:59 am

      for example, you need to call models that registered in SM in your master module from transaction module.

  3. Rocco said, on June 11, 2013 at 11:56 pm

    Hi,
    i trying working with subModules.

    For example:

    /MyModule
    -Module.php
    -/src
    —/MyModule
    ——-/Controller
    ——-/Form
    ——-/Model
    —/MySubModule1
    ——-/Controller
    ——-/Form
    ——-/Model

    In Module.php i define a single namespace called “MyModule”…

    namespace MyModule;

    use MyModule\Model\MyModule;
    use MyModule\Model\MyModuleTable;

    ///-> use MySubModule1\Model\MySubModule1; ?!?!?!?!? is it correct?
    ///-> use MySubModule1\Model\MySubModule1Table; ?!?!?!??!?!

    class Module
    {
    public function getAutoloaderConfig()
    {
    return array(
    ‘Zend\Loader\ClassMapAutoloader’ => array(
    __DIR__ . ‘/autoload_classmap.php’,
    ),
    ‘Zend\Loader\StandardAutoloader’ => array(
    ‘namespaces’ => array(
    __NAMESPACE__ => __DIR__ . ‘/src/’ . __NAMESPACE__,
    // And this is for the custom namespace
    ‘MySubModule1’ => __DIR__ . ‘/src/’ . ‘MySubModule1’,
    ),
    ),
    );
    }

    public function getConfig()
    {
    return include __DIR__ . ‘/config/module.config.php’;
    }

    public function getServiceConfig()
    {
    return array(
    ‘factories’ => array(
    ‘MyModule\Model\MyModuleTable’ => function($sm){
    $dbAdapter = $sm->get(‘Zend\Db\Adapter\Adapter’);
    $cacheAdapter = $sm->get(‘Zend\Cache\Storage\Filesystem’);

    $table = new Model\MyModuleTable($dbAdapter);
    $table->setCache($cacheAdapter);

    return $table;
    },

    ‘MySubModule1\Model\MySubModule1Table’ => function($sm){
    $dbAdapter = $sm->get(‘Zend\Db\Adapter\Adapter’);
    $cacheAdapter = $sm->get(‘Zend\Cache\Storage\Filesystem’);

    $table = new Model\MySubModule1Table($dbAdapter); /////// -> CLASS NOT FOUND
    $table->setCache($cacheAdapter);

    return $table;
    },

    ),
    );
    }

    I have defined multple namespase in the getAutoloaderConfig() method but the class do not recognize the namespace MySubModule1 because exist a single class namespace MyModule.

    Is it a correct way for manage submodules ?

    Excuse me and thanks in advance.

  4. […] ссылка на оригинал […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: