# Where to register EventListeners to work via MVC and CLI

**URL:** https://discourse.laminas.dev/t/where-to-register-eventlisteners-to-work-via-mvc-and-cli/3233
**Category:** Components & MVC
**Tags:** laminas-eventmanager, laminas-cli, laminas-mvc
**Created:** [March 8, 2023, 3:03pm UTC](https://discourse.laminas.dev/t/where-to-register-eventlisteners-to-work-via-mvc-and-cli/3233 "2023-03-08T15:03:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![FalkHe](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/falkhe/32/1296_2.png) [@FalkHe](https://discourse.laminas.dev/u/FalkHe)
#### Post date: [March 8, 2023, 3:03pm UTC](https://discourse.laminas.dev/t/where-to-register-eventlisteners-to-work-via-mvc-and-cli/3233/1 "2023-03-08T15:03:56Z")

</div>

Looks like EventListeners are totally ignored when code is executed via laminas-cli.

The Reason is that neither Module::onBoostrap() nor the config key ‘listeners’ is handled by lamainas-cli.

But what’s the alternative? Where in a Laminas Module can EventListeners be configured / attached savely?

Looks like Module::init() ist the only place where code is excecuted in any case. But from this method there’s no access to the Service-Manager an therefor no way to fetch and attach the Listeners. 😕

Usecase:  
On each module, i have a ServiceLayer that triggers events to the EventManager. (i.E. ‘model-xy::entity:saved’, ‘session:sign-in’, ‘bussines-process:abc:finished’, …)

In other modules i need to attach to those Events. Not only on MVC Requests but also on laminas-cli calls. i.E. ‘busines-prozess:abc:finished’ might happen via MVC but might also happen via laminas-cli.

Any suggestion is appreciated

Thanks in advance

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [March 8, 2023, 3:14pm UTC](https://discourse.laminas.dev/t/where-to-register-eventlisteners-to-work-via-mvc-and-cli/3233/2 "2023-03-08T15:14:42Z")

</div>

Please check the issue tracker of laminas-cli there will you find your answer. For example:

> <https://github.com/laminas/laminas-cli/issues/106>
>
> \### Bug Report
> 
> | Q | A
> |------------ | ------
> | Version(s) | 1….7.0
> 
> \#### Summary
> 
> When using \`laminas/laminas-cli\` in the context of a MvcApplication (\`ContainerResolver::resolveMvcContainer\`), modules don't get bootstrapped completely, which results in services potentially missing configuration.
> 
> \#### Current behavior
> 
> We use the \`onBootstrap\` event of our MvcApplication to set up listeners for services or set up service state based on context.
> When using those services inside of commands executed by \`laminas/laminas-cli\`, those services are not fully set up, which results in unexpected behavior, since the application/module is missing the \`bootstrap\` lifecycle stage.
> 
> Based on the \[Documentation\](https://docs.laminas.dev/laminas-mvc/examples/#bootstrapping), the \`onBootstrap\` method should/could be used for:
> \* module-specific configuration
> \* setup event listeners for you module
> 
> \#### How to reproduce
> 
> Use a service somehow modified or configured in the \`onBootstrap\` method in a \`Command\` executed through \`laminas/laminas-cli\`.
> The service passed to the command has not gone through the MvcApplications bootstrap lifecycle.
> 
> I try to illustrate this on the following pseudo-module:
> 
> \`\`\`php
> // Module
> class Module implements BootstrapListenerInterface
> {
> public function onBootstrap(EventInterface $e)
> {
> $sm = $e-\>getApplication()-\>getServiceManager();
>         
> $sm-\>get(SampleService::class)-\>setBootstrapped(true);
> }
> }
> \`\`\`
> 
> \`\`\`php
> // in SampleCommandFactory
> public function \_\_invoke(ContainerInterface $container, $requestedName, ?array $options = null)
> {
> return new SampleCommand(
> $container-\>get(SampleService::class)
> );
> }
> \`\`\`
> 
> \`\`\`php
> // in SampleCommand
> protected function execute(InputInterface $input, OutputInterface $output) : int
> {
> return $this-\>sampleService-\>isBootstrapped() ? 0 : 1;
> }
> \`\`\`
> 
> \`SampleCommand\` returns \`1\`.
> 
> \#### Expected behavior
> 
> From my perspective, using commands through \`laminas/laminas-cli\` in the context of an MvcApplication, should guarantee the same initialisation level as with \`Application::init\`.
> 
> In the above example, \`SampleCommand\` should return \`0\`, as it has access to a bootstrapped version of the \`SampleService\`.

But apparently no one is interested in solving the problem, but only in demanding that it _must_ work. 🤷🏻‍♂️

---

<div class="post-metadata">

### Author: ![FalkHe](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/falkhe/32/1296_2.png) [@FalkHe](https://discourse.laminas.dev/u/FalkHe)
#### Post date: [March 8, 2023, 3:57pm UTC](https://discourse.laminas.dev/t/where-to-register-eventlisteners-to-work-via-mvc-and-cli/3233/3 "2023-03-08T15:57:38Z")

</div>

Got it, thanks.

I’ll give it a try …

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [March 9, 2023, 11:04am UTC](https://discourse.laminas.dev/t/where-to-register-eventlisteners-to-work-via-mvc-and-cli/3233/4 "2023-03-09T11:04:03Z")

</div>

Thank you for pushing the topic! 👍🏻

> <https://github.com/laminas/laminas-cli/pull/110>
>
> | Q | A
> |-------------- | ------
> | Documentation | yes
> | Bugfix… | yes
> | BC Break | no
> | New Feature | yes
> | RFC | yes/no
> | QA | yes/no
> 
> \### Description
> 
> Provide option to enable MVC Application bootstrapping.
> 
> False by default, to not break any existing app.
> 
> See Issue #106 .
