Zend Framework 2 : Check Module Dependency
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
That’s a good news, thanks 🙂
You’re welcome 😉
Can you give me a example of a situation use this? I’m new with ZF2, thx.
for example, you need to call models that registered in SM in your master module from transaction module.
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.
try this : http://ctrl-f5.net/php/zend-framework-2-modules-with-subnamespaces/
thanks as always, but this is a good solution? It seems to me very confused!
It would be a good thing because a module can have submodules.
that’s subjective matter.
[…] ссылка на оригинал […]