2
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() is working fine in ZF2 BETA5 but it is not working in stable final release.

Has this functionality taken off in final release?
How can I make this work in ZF2 final release?

Florent
  • 12,310
  • 10
  • 49
  • 58
Developer
  • 25,073
  • 20
  • 81
  • 128
  • What does not work? Any error message? (I started with RC1.) – Daniel M Sep 13 '12 at 12:45
  • Btw, works here. Are you loading the module? – Daniel M Sep 13 '12 at 12:51
  • no error message. yes I am loading the module and also login page under Auth namespace is working fine. even init() is executing fine too. – Developer Sep 13 '12 at 12:54
  • if I switch directory of zf2 final release to zf2 beta5. it start working. why not working in zf2 final release? – Developer Sep 13 '12 at 12:56
  • Ok, is there maybe another event handler with a higher priority for this event which stops the propagation? I don't know why it's not working for your with the 2.0.0 release. I've tested it with a freshly pulled ZF2 from git and it works. – Daniel M Sep 13 '12 at 12:56
  • I have install the zf2 using composer when there was beta 1 since than I have not run composer for update I am only downloading the zf2 framework menually and overwriting the preivous zf dictory in vender folder. Do I need to run the composer for update? would this make any difference? I have also been using doctrine2 with zf2. I have done quite a lot development on zf2. I can't get my head around this problem. – Developer Sep 13 '12 at 13:12
  • I don't think you need to run compose (I don't either). You'll probably have to do some debugging. Check if the event is registered correctly, what events are triggered, which handlers are invoked etc. You have to go to the library classes to do that and either put some debug var_dumps where necessary or debug it using XDebug or Zend Debugger. – Daniel M Sep 13 '12 at 13:22
  • Its pain. I will keep developing over beta 5 till i get some time to debug stable release which is in fact a unstable ... :( – Developer Sep 13 '12 at 16:12
  • You don't want to do that! There are several API changes since the beta and even during the early RCs. You *really* want to solve that problem (maybe some other way) and continue on the recent code base of ZF2. – Daniel M Sep 13 '12 at 16:13
  • what a waste of time. zf2 final release change the way it read folder structure. read answer below – Developer Sep 13 '12 at 18:29

3 Answers3

1
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);
}
samsonasik
  • 440
  • 1
  • 6
  • 11
0

In Beta Series of zend framework2

Auth\src\User\Controller\UserController.php

but in final release of zf2 this does not work. Main namespace folder should match exactly same as under src folder. so above will work only like this

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

Don't forget to change your namespaces and paths in module.php and module.config.php and controller file.

Developer
  • 25,073
  • 20
  • 81
  • 128
0

There are two ways,

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

Or from onBootstrap method again in Module.php but not from ModuleManager but by the application object as Abdul did.

Remember, init and onBoostrap methods run for every page request. Registering Event there is okay but do not put heavy stuff there. I prefer sharedEventManager, as it is available even if the service is initializes in future.

Cheers!

khunshan
  • 2,662
  • 4
  • 28
  • 34