|
| 1 | +# Automating Controller Factories |
| 2 | + |
| 3 | +Writing a factory class for each and every controller that has dependencies |
| 4 | +can be tedious, particularly in early development as you are still sorting |
| 5 | +out dependencies. |
| 6 | + |
| 7 | +As of version 3.0.1, zend-mvc ships with `Zend\Mvc\Controller\LazyControllerFactory`, |
| 8 | +which provides a reflection-based approach to controller instantiation, |
| 9 | +resolving constructor dependencies to the relevant services. The factory may be |
| 10 | +used as either an abstract factory, or mapped to specific controller names as a |
| 11 | +factory: |
| 12 | + |
| 13 | +```php |
| 14 | +use Zend\Mvc\Controller\LazyControllerFactory; |
| 15 | + |
| 16 | +return [ |
| 17 | + /* ... */ |
| 18 | + 'controllers' => [ |
| 19 | + 'abstract_factories' => [ |
| 20 | + LazyControllerFactory::class, |
| 21 | + ], |
| 22 | + 'factories' => [ |
| 23 | + 'MyModule\Controller\FooController' => LazyControllerFactory::class, |
| 24 | + ], |
| 25 | + ], |
| 26 | + /* ... */ |
| 27 | +]; |
| 28 | +``` |
| 29 | + |
| 30 | +Mapping controllers to the factory is more explicit and performant. |
| 31 | + |
| 32 | +The factory operates with the following constraints/features: |
| 33 | + |
| 34 | +- A parameter named `$config` typehinted as an array will receive the |
| 35 | + application "config" service (i.e., the merged configuration). |
| 36 | +- Parameters typehinted against array, but not named `$config`, will |
| 37 | + be injected with an empty array. |
| 38 | +- Scalar parameters will be resolved as null values. |
| 39 | +- If a service cannot be found for a given typehint, the factory will |
| 40 | + raise an exception detailing this. |
| 41 | +- Some services provided by Zend Framework components do not have |
| 42 | + entries based on their class name (for historical reasons); the |
| 43 | + factory contains a map of these class/interface names to the |
| 44 | + corresponding service name to allow them to resolve. These include: |
| 45 | + - `Zend\Console\Adapter\AdapterInterface` maps to `ConsoleAdapter`, |
| 46 | + - `Zend\Filter\FilterPluginManager` maps to `FilterManager`, |
| 47 | + - `Zend\Hydrator\HydratorPluginManager` maps to `HydratorManager`, |
| 48 | + - `Zend\InputFilter\InputFilterPluginManager` maps to `InputFilterManager`, |
| 49 | + - `Zend\Log\FilterPluginManager` maps to `LogFilterManager`, |
| 50 | + - `Zend\Log\FormatterPluginManager` maps to `LogFormatterManager`, |
| 51 | + - `Zend\Log\ProcessorPluginManager` maps to `LogProcessorManager`, |
| 52 | + - `Zend\Log\WriterPluginManager` maps to `LogWriterManager`, |
| 53 | + - `Zend\Serializer\AdapterPluginManager` maps to `SerializerAdapterManager`, |
| 54 | + - `Zend\Validator\ValidatorPluginManager` maps to `ValidatorManager`, |
| 55 | + |
| 56 | +`$options` passed to the factory are ignored in all cases, as we cannot |
| 57 | +make assumptions about which argument(s) they might replace. |
| 58 | + |
| 59 | +Once your dependencies have stabilized, we recommend writing a dedicated |
| 60 | +factory, as reflection can introduce performance overhead. |
| 61 | + |
| 62 | +## References |
| 63 | + |
| 64 | +This feature was inspired by [a blog post by Alexandre Lemaire](http://circlical.com/blog/2016/3/9/preparing-for-zend-f). |
0 commit comments