zendframework/zend-mvc
Zend\Mvc is Zend Framework’s MVC layer for building PHP web apps. It provides routing, controllers, dispatching, request/response handling, view integration, and an event-driven pipeline. Designed for modular apps and flexible configuration.
Writing a factory class for each and every controller that has dependencies can be tedious, particularly in early development as you are still sorting out dependencies.
As of version 3.0.1, zend-mvc ships with Zend\Mvc\Controller\LazyControllerAbstractFactory,
which provides a reflection-based approach to controller instantiation,
resolving constructor dependencies to the relevant services. The factory may be
used as either an abstract factory, or mapped to specific controller names as a
factory:
use Zend\Mvc\Controller\LazyControllerAbstractFactory;
return [
/* ... */
'controllers' => [
'abstract_factories' => [
LazyControllerAbstractFactory::class,
],
'factories' => [
'MyModule\Controller\FooController' => LazyControllerAbstractFactory::class,
],
],
/* ... */
];
Mapping controllers to the factory is more explicit and performant.
The factory operates with the following constraints/features:
$config typehinted as an array will receive the
application "config" service (i.e., the merged configuration).$config, will
be injected with an empty array.Zend\Console\Adapter\AdapterInterface maps to ConsoleAdapter,Zend\Filter\FilterPluginManager maps to FilterManager,Zend\Hydrator\HydratorPluginManager maps to HydratorManager,Zend\InputFilter\InputFilterPluginManager maps to InputFilterManager,Zend\Log\FilterPluginManager maps to LogFilterManager,Zend\Log\FormatterPluginManager maps to LogFormatterManager,Zend\Log\ProcessorPluginManager maps to LogProcessorManager,Zend\Log\WriterPluginManager maps to LogWriterManager,Zend\Serializer\AdapterPluginManager maps to SerializerAdapterManager,Zend\Validator\ValidatorPluginManager maps to ValidatorManager,$options passed to the factory are ignored in all cases, as we cannot
make assumptions about which argument(s) they might replace.
Once your dependencies have stabilized, we recommend writing a dedicated factory, as reflection can introduce performance overhead.
This feature was inspired by a blog post by Alexandre Lemaire.
How can I help you explore Laravel packages today?