$ moduleManager-> getEventManager () & # 8594; getSharedManager () & # 8594; attach does not work in stable zf2

namespace Auth; use Zend\ModuleManager\ModuleManager; class Module { public function init(ModuleManager $moduleManager) { $sharedEvents = $moduleManager->getEventManager()->getSharedManager(); $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) { echo "I am init module dispatch"; exit(); }, 100); } } 

$moduleManager->getEventManager()->getSharedManager()->attach() works fine in ZF2 BETA5, but it doesn't work in the stable final release.

Did this functionality work in the final version?
How can I make this work in the final version of ZF2?

+2
source share
3 answers

Zend framework2 beta

 Auth\src\User\Controller\UserController.php 

but in the final version of zf2 this does not work. The main namespace folder must match exactly as in the src folder. so the above will only work like this:

 Auth\src\Auth\Controller\UserController.php or User\src\User\Controller\UserController.php 

Remember to change the namespaces and paths in the module.php and module.config.php and controller files.

0
source
 public function onBootstrap(MvcEvent $e) { $application = $e->getApplication(); $sharedManager = $application->getEventManager()->getSharedManager(); $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) { echo "I am init module dispatch"; exit(); }, 100); } 
+1
source

There are two ways:

You can get it from the init.php init method by passing it a ModuleManger object, and then modulemanager-> getEventManager.

Or from the onBootstrap method again to Module.php, but not from ModuleManager, but using the application object, as Abdul did.

Remember that init and onBoostrap methods are triggered for each page request. Logging events there is good, but don't put heavy things in there. I prefer sharedEventManager as it is available even if the service is initializing in the future.

Hooray!

0
source

Source: https://habr.com/ru/post/1445753/


All Articles