# Blog post: Laminas Event Manager and Lazy listeners

**URL:** <https://discourse.laminas.dev/t/blog-post-laminas-event-manager-and-lazy-listeners/2029>\
**Category:** Show and Tell\
**Tags:** laminas-eventmanager, laminas-mvc\
**Created:** [December 30, 2020, 7:45pm UTC](https://discourse.laminas.dev/t/blog-post-laminas-event-manager-and-lazy-listeners/2029 "2020-12-30T19:45:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomstryja](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tomstryja/32/1075_2.png) [@tomstryja](https://discourse.laminas.dev/u/tomstryja)\
**Post date:** [December 30, 2020, 7:45pm UTC](https://discourse.laminas.dev/t/blog-post-laminas-event-manager-and-lazy-listeners/2029/1 "2020-12-30T19:45:19Z")

</div>

Post about Laminas Event Manager with some examples of implementations. Any feedback is appreciated.

- [Laminas Event Manager and Lazy listeners](https://www.we-rc.com/blog/2020/11/11/laminas-event-manager-and-lazy-listeners)

---

<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:** [December 31, 2020, 11:36am UTC](https://discourse.laminas.dev/t/blog-post-laminas-event-manager-and-lazy-listeners/2029/2 "2020-12-31T11:36:03Z")

</div>

Thanks for creating and sharing your blog post! 👍

> [@tomstryja](#):
>
> Any feedback is appreciated.

My first comment is related to your listeners: never inject the entire service manager in a class. If you do that, then you have a design fault from the start and you are on the best way to an anti-pattern. Your class should not even know that there is a dependency injection container. Give only what is necessary.

The problem can be found in `User\Event\EventLogListener` and in `Order\Event\HideDiscountListener`.

My second hint is related to the module class of `Application`. The method `onPreRoute` can be strongly reduced:

1. The method `Application\Module::onPreRoute()` is called on every request and on every request the language code is saved in a session. This is unnecessary, as a session should be used to transport information across multiple requests, which is not the case here.
2. In all Laminas and Mezzio projects it is recommended to use [laminas-component-installer](https://docs.laminas.dev/laminas-component-installer/) via global installation. If you then install [laminas-mvc-i18n](https://github.com/laminas/laminas-mvc-i18n) the translator will be set for the router. No further steps are necessary because `Laminas\Mvc\I18n\Router\HttpRouterDelegatorFactory` is used then.
3. There is no need to fetch the translator from the container. You can use PHP’s `Locale::setDefault('…')` and the translator and all helpers of laminas-i18n will use the set value. (This also allows you to retrieve the current locale via [`Locale::getDefault()`](https://www.php.net/manual/locale.getdefault.php) within your entire application.)
4. Adding the translation files in this way is unnecessary because adding a translation file does not mean it will be loaded in the same step. The corresponding file is only loaded when a translation is retrieved. You can use the module config or the global config file to add translation files.

* * *

Another [example of the use of listeners](https://docs.laminas.dev/laminas-view/cookbook/setting-module-specific-layouts/) can be found in the laminas-view documentation.

---

<div class="post-metadata">

**Author:** ![tomstryja](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tomstryja/32/1075_2.png) [@tomstryja](https://discourse.laminas.dev/u/tomstryja)\
**Post date:** [January 4, 2021, 4:05pm UTC](https://discourse.laminas.dev/t/blog-post-laminas-event-manager-and-lazy-listeners/2029/3 "2021-01-04T16:05:48Z")

</div>

> [@froschdesign](#):
>
> never inject the entire service manager in a class

Thank you for reply froschdesign …

Yes, I’m aware of this anti-pattern. But in example the `UsersActionsLogTable` model was “wraped” with delegator class also. And because listener is registered during onBootstrap, so the `UsersActionsLogTable` class is not decorated with decorator class during this proces yet … If I’m right.

(Decorator was setting identity property, but this property was still empty in the model `UsersActionsLogTable`. So I injected Service Manager class in listener construct instead of `UsersActionsLogTable` and access the model later.)

---

<div class="post-metadata">

**Author:** ![tomstryja](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tomstryja/32/1075_2.png) [@tomstryja](https://discourse.laminas.dev/u/tomstryja)\
**Post date:** [January 4, 2021, 4:26pm UTC](https://discourse.laminas.dev/t/blog-post-laminas-event-manager-and-lazy-listeners/2029/4 "2021-01-04T16:26:29Z")

</div>

> [@froschdesign](#):
>
> use PHP’s `Locale::setDefault('…')` and the translator and all helpers of laminas-i18n will use the set value

Hm, didn’t know about this 👍

---

<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:** [January 5, 2021, 1:32pm UTC](https://discourse.laminas.dev/t/blog-post-laminas-event-manager-and-lazy-listeners/2029/5 "2021-01-05T13:32:54Z")

</div>

> [@tomstryja](#):
>
> Hm, didn’t know about this 👍

See:

> **[Translation - laminas-i18n - Laminas Docs](https://docs.laminas.dev/laminas-i18n/translation/#setting-a-locale)**
>
> Provide translations for your application, and filter and validate internationalized values.
