Closed Swader closed 9 years ago
Loosely, Pimple is too simple for what you want. Personally, I suspect that you'd be better off with an auto-wiring DI container such as PHP.DI.
Another way would be use Zend\ServiceManager's initializers to add the common dependencies via setters. You still need to tell the DI container the name of every controller, though.
If you don't want the DI Container to know the names of the controller in advance, then I would use Zend\ServiceManager's abstract factories so that a single factory that can create all the controllers. If I get time, I'll see if I can knock up an example that does this at some point
Thanks, I'll look into all that.
The original idea was mapping class name patterns to callbacks, so that when a pattern like "Controller" is detected, unless there is a mapping that corresponds to the controller name exactly, the default takes over and constructs with the standard view
and logger
.
Extending Pimple and Slim's Container in such a way proved to require way more work than it should have, though, and just using a different DI container seemed more logical.
OK, The Abstract Factory solution was easy to create an example for - https://github.com/akrabat/slim3-abstract-action-factory
The work is done here: https://github.com/akrabat/slim3-abstract-action-factory/blob/master/app/src/ActionAbstractFactory.php
That looks great, thanks. I'll take a more detailed look soon.
I tried to implement the following abstract factory, but its not working for me. i'm getting 'addAbstractFactory' methode is not defined. i installed 'zendframework/zend-servicemanager' and 'akrabat/rka-slim-zfsm-container' via composer after cloning 'akrabat/slim3-skeleton'. and made the required changes in two files as sited in slim3-abstract-action-factory github project. am i missing something else ? thanks
Are you using Zend\ServiceManager
. I usually set it up in index.php: https://github.com/akrabat/slim3-abstract-action-factory/blob/master/public/index.php#L18-L19
yep i modified two files, the index.php to instantiate the object and the dependencies.php :/
As per discussion in this Tweet stream.
Defining a new factory for every controller separately seems wasteful, especially when most might share some, if not all dependencies.
What would be good approaches to handling this? Originally, I thought about something like this:
And then extending the
get
andhas
methods as necessary. But this seems a bit clumsy and too much work for such a simple feature - especially considering a newoffsetExists
would need to be added into the mix, too, and some other things.Suggestions?