Facade in laravel style laminas

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

Hello and welcome to our forums! :smiley:

I don’t think that will work:

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:

1 Like

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

1 Like

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 :smiley:

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

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.

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:

@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?

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

This abstract factory is used in the tutorial “laminas-form: Usage in a laminas-mvc Application”, for example.
This abstract factory can be used for all controllers and eliminates the need to register controllers individually:

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

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

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:

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

Many thanks, I’ll dig into it.