Hi, Evan gave me a hint about that already and I got it working.
But thank you! It made the whole thing more clear. To get to the beginning of this thread, the whole purpose to inject the forms via EventManager was done that we don´t have to get them from the ServiceManager, right? Because Di is always better then to pull them from the SM in the controller and as such we are dependent on the sm. But the controllers are SM aware already, why is this better? -----Ursprüngliche Nachricht----- Von: Ralf Eggert [mailto:[email protected]] Gesendet: Dienstag, 25. September 2012 13:19 An: [email protected] Betreff: Re: [fw-general] [ZF2] Injecting objects to a controller, or getting objects from the service locator? Hi Marc, you can use the shared event manager like this (reduced to one form): -------------------------------------------------------------------------- /module/User/Module.php class Module { protected $serviceLocator = null; public function onBootstrap(EventInterface $e) { [...] $this->serviceLocator = $e->getApplication() ->getServiceManager(); $sharedEventManager = $this->serviceLocator ->get('SharedEventManager'); $sharedEventManager->attach( 'User\Service\User', 'set-user-register-form', array($this, 'onRegisterForm') ); } public function getServiceConfig() { return array( 'factories' => array( 'User\Form\UserRegister' => 'User\Form\UserRegister', 'User\Service\User' => 'User\Service\UserFactory', ), ); } public function onRegisterForm(EventInterface $e) { $service = $this->serviceLocator->get('User\Service\User'); $form = $this->serviceLocator->get('User\Form\UserRegister'); $service->setRegisterForm($form); } } -------------------------------------------------------------------------- In the onBootstrap() method I attach the event 'set-user-register-form' to my service 'User\Service\User'. When this event is triggered, then the onRegisterForm() method of the Module object should be called. The onRegisterForm() method just takes the user service and the form and calls the setRegisterForm() method of my user service. -------------------------------------------------------------------------- /module/User/src/User/Service/User.php class User implements EventManagerAwareInterface { protected $eventManager = null; protected $registerForm; public function setEventManager(EventManagerInterface $eventManager) { $eventManager->setIdentifiers(array(__CLASS__)); $this->eventManager = $eventManager; } public function getEventManager() { return $this->eventManager; } public function getRegisterForm() { if (null === $this->registerForm) { $this->getEventManager()->trigger('set-user-register-form'); $this->setMessage('user_message_info_user_add'); } return $this->registerForm; } public function setRegisterForm(UserSaveForm $form) { $this->registerForm = $form; } } -------------------------------------------------------------------------- With the implementation of EventManagerAwareInterface the Event Manager is injected to my user service automatically. When I call the getRegisterForm() method of my user service for the first time, the event 'set-user-register-form' is triggered. Now the event manager calls Module::onRegisterForm() which creates my register form and passes it to my user service. You can build this for all forms you have to just create form instances when you really need it. Regards, Ralf -- List: [email protected] Info: http://framework.zend.com/archives Unsubscribe: [email protected] -- List: [email protected] Info: http://framework.zend.com/archives Unsubscribe: [email protected]
