# Facade in laravel style laminas

**URL:** <https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494>\
**Category:** 3rd-Party Tools & Libraries\
**Tags:** laminas-mvc\
**Created:** [October 12, 2023, 5:02am UTC](https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494 "2023-10-12T05:02:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jesus\_vieira](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jesus_vieira/32/1503_2.png) [@jesus\_vieira](https://discourse.laminas.dev/u/jesus_vieira)\
**Post date:** [October 12, 2023, 5:02am UTC](https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494/1 "2023-10-12T05:02:06Z")

</div>

Hello, I made some FACADEs to facilitate development.

It was based on laravel facade, it’s just a test model that I’m using in some projects.

And I decided to share it with the community, if anyone is interested, here is the project:

[https://github.com/juizmill/laminas-facade](https://github.com/juizmill/laminas-facade)

---

<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:** [October 12, 2023, 6:53am UTC](https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494/2 "2023-10-12T06:53:27Z")

</div>

Hello and welcome to our forums! 😃

I don’t think that will work:

```php
LaminasFacade\Facades\EventManager::attach('test', static function () {
    echo 'test';
});
LaminasFacade\Facades\EventManager::trigger('test');

```

When calling up one of your _facades_, a completely new application is created each time, this is a disaster - unfortunately I cannot call it anything else:

> <https://github.com/juizmill/laminas-facade/blob/7be7e0de0985a56af0afc2e5bf5c352cdf5ff5ce/src/Facades/AbstractFacade.php#L38-L47>

---

<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:** [October 12, 2023, 6:54am UTC](https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494/3 "2023-10-12T06:54:22Z")

</div>

Regardless of the problem, what was the motivation for the implementation?

---

<div class="post-metadata">

**Author:** ![jesus\_vieira](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jesus_vieira/32/1503_2.png) [@jesus\_vieira](https://discourse.laminas.dev/u/jesus_vieira)\
**Post date:** [October 12, 2023, 5:58pm UTC](https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494/4 "2023-10-12T17:58:25Z")

</div>

I agree that it is bad to call the application this way, this was the only way I found to make the AbstractFacade class work. If you want to help improve it, feel free 😃

The motivation is to call some system functionalities in a simple and quick way instead of making several configurations and implementations.

---

<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:** [October 12, 2023, 6:26pm UTC](https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494/5 "2023-10-12T18:26:25Z")

</div>

> [@jesus\_vieira](#):
>
> I agree that it is bad to call the application this way, this was the only way I found to make the AbstractFacade class work.

**But it does not work!** You always create a new application, which also means that you also create a new instance of the event manager, service manager, etc. each time.  
Therefore, the listener for the `test` event from the example above is never (!) called.

> [@jesus\_vieira](#):
>
> The motivation is to call some system functionalities in a simple and quick way instead of making several configurations and implementations.

Just because the thing is called a facade in Laravel doesn’t mean it’s a facade, and the idea of calling a static function everywhere isn’t any better.  
Use the constructor and let the framework help you, then you are on the right way and also quick to implement it – this works in Laravel, Laminas, Symfony, CakePHP, etc.

It’s understandable that writing a factory class for every single service that has dependencies can be tedious. Therefore, “auto wiring” will help you.

See in the tutorials for some examples:

> **[tutorials - Laminas Docs](https://docs.laminas.dev/tutorials/#application-integrations)**
>
> Learn how to create laminas-mvc applications, get in-depth guides into components, and discover how to migrate your applications to version 3!

---

<div class="post-metadata">

**Author:** ![Serge\_Nguimjeu](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/serge_nguimjeu/32/562_2.png) [@Serge\_Nguimjeu](https://discourse.laminas.dev/u/Serge_Nguimjeu)\
**Post date:** [October 18, 2023, 2:43pm UTC](https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494/6 "2023-10-18T14:43:38Z")

</div>

> [@froschdesign](#):
>
> It’s understandable that writing a factory class for every single service that has dependencies can be tedious. Therefore, “auto wiring” will help you.

@froschdesign If I understood correctly laminas/mezzio supports “auto-wiring”. I was not aware of that. Can you please point me to the specific documentation on that?

---

<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:** [October 18, 2023, 6:53pm UTC](https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494/7 "2023-10-18T18:53:35Z")

</div>

> [@Serge\_Nguimjeu](#):
>
> Can you please point me to the specific documentation on that?

If you look at the tutorials linked above you will find `Laminas\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory`:

> **[Reflection-based Abstract Factory - laminas-servicemanager - Laminas Docs](https://docs.laminas.dev/laminas-servicemanager/reflection-abstract-factory/)**
>
> laminas-servicemanager: factory-driven dependency injection container

This abstract factory is used in the [tutorial “laminas-form: Usage in a laminas-mvc Application”](https://docs.laminas.dev/laminas-form/v3/application-integration/usage-in-a-laminas-mvc-application/#register-form-and-controller), for example.  
This abstract factory can be used for all controllers and eliminates the need to register controllers individually:

```php
return [
    'controllers' => [
        'abstract_factories' => [
            Laminas\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory::class,
        ],
    ],
    // …
];

```

Another option is to use a CLI command to create your factories:

> **[Console Tools - laminas-servicemanager - Laminas Docs](https://docs.laminas.dev/laminas-servicemanager/console-tools/#generate-factory-for-class)**
>
> laminas-servicemanager: factory-driven dependency injection container

Version 4 of laminas-servicemanager will bring some more CLI commands. One of these will replace the usage of `ReflectionBasedAbstractFactory` with a real factory class which can be beneficial for the performance:

> <https://github.com/laminas/laminas-servicemanager/blob/c902c4144e491d9cb2b3b652db3ac5cfc96abf42/src/Command/AheadOfTimeFactoryCreatorCommand.php#L48-L61>

Another option is a third-party library which comes with caching and more:

> **[GitHub - reinfi/zf-dependency-injection: Advanced dependency injection for...](https://github.com/reinfi/zf-dependency-injection)**
>
> Advanced dependency injection for laminas framework - GitHub - reinfi/zf-dependency-injection: Advanced dependency injection for laminas framework

---

<div class="post-metadata">

**Author:** ![Serge\_Nguimjeu](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/serge_nguimjeu/32/562_2.png) [@Serge\_Nguimjeu](https://discourse.laminas.dev/u/Serge_Nguimjeu)\
**Post date:** [October 19, 2023, 6:40am UTC](https://discourse.laminas.dev/t/facade-in-laravel-style-laminas/3494/8 "2023-10-19T06:40:14Z")

</div>

Many thanks, I’ll dig into it.
