Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit df3000d

Browse files
committed
Added documentation for LazyControllerFactory
1 parent 8e2e226 commit df3000d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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).

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pages:
1818
- 'v2.X to v2.7': migration/to-v2-7.md
1919
- 'v2.X to v3.0': migration/to-v3-0.md
2020
- Cookbook:
21+
- 'Automating controller factories': cookbook/automating-controller-factories.md
2122
- 'Using middleware within event listeners': cookbook/middleware-in-listeners.md
2223
site_name: zend-mvc
2324
site_description: 'zend-mvc: MVC application provider'

0 commit comments

Comments
 (0)