@@ -11,127 +11,45 @@ injection container with the following features:
11
11
- instance factories.
12
12
- optional auto-resolution of typehinted constructor parameter values.
13
13
14
- ## Installing Aura.Di
14
+ ## Installing and configuring Aura.Di
15
15
16
16
Aura.Di implements [ container-interop] ( https://github.com/container-interop/container-interop )
17
- as of version 3.
17
+ as of version 3. To use Aura.Di as a dependency injection container, we
18
+ recommend using [ zendframework/zend-auradi-config] ( https://github.com/zendframework/zend-auradi-config ) ,
19
+ which helps you to configure its container. First install the package:
18
20
19
21
``` bash
20
- $ composer require aura/di
22
+ $ composer require zendframework/zend-auradi-config
21
23
```
22
24
23
- ## Configuration
24
-
25
- Aura.Di can help you to organize your code better with
26
- [ ContainerConfig classes] ( http://auraphp.com/packages/Aura.Di/config.html ) and
27
- [ two step configuration] ( http://auraphp.com/blog/2014/04/07/two-stage-config/ ) .
28
- In this example, we'll put that in ` config/container.php ` :
25
+ To configure Aura.Di, create the file ` config/container.php ` with the following
26
+ contents:
29
27
30
28
``` php
31
29
<?php
32
- use Aura\Di\ContainerBuilder;
30
+ use Zend\AuraDi\Config\Config;
31
+ use Zend\AuraDi\Config\ContainerFactory;
33
32
34
- $containerBuilder = new ContainerBuilder();
33
+ $config = require __DIR__ . '/config.php';
34
+ $factory = new ContainerFactory();
35
35
36
- // Use the builder to create and configure a container using an array of
37
- // ContainerConfig classes. Make sure the classes can be autoloaded!
38
- return $containerBuilder->newConfiguredInstance([
39
- 'Application\Config\Common',
40
- ]);
36
+ return $factory(new Config($config));
41
37
```
42
38
43
- The bare minimum ` ContainerConfig ` code needed to make zend-expressive work is:
44
-
45
- ``` php
46
- <?php
47
- // In src/Config/Common.php:
48
- namespace Application\Config;
49
-
50
- use Aura\Di\Container;
51
- use Aura\Di\ContainerConfig;
52
- use Aura\Router\Generator;
53
- use Aura\Router\RouteCollection;
54
- use Aura\Router\RouteFactory;
55
- use Aura\Router\Router;
56
- use Zend\Escaper\Escaper;
57
- use Zend\Expressive\Application;
58
- use Zend\Expressive\Container as ExpressiveContainer;
59
- use Zend\Expressive\Delegate;
60
- use Zend\Expressive\Middleware;
61
- use Zend\Expressive\Plates\PlatesRenderer;
62
- use Zend\Expressive\Router\AuraRouter;
63
- use Zend\Expressive\Router\Route;
64
- use Zend\Expressive\Router\RouterInterface;
65
- use Zend\Expressive\Template\TemplateRendererInterface;
66
-
67
- class Common extends ContainerConfig
68
- {
69
- public function define(Container $di)
70
- {
71
- $di->params[RouteCollection::class] = array(
72
- 'route_factory' => $di->lazyNew(RouteFactory::class),
73
- );
74
- $di->params[Router::class] = array(
75
- 'routes' => $di->lazyNew(RouteCollection::class),
76
- 'generator' => $di->lazyNew(Generator::class),
77
- );
78
- $di->params[AuraRouter::class]['router'] = $di->lazyNew(Router::class);
79
- $di->set(RouterInterface::class, $di->lazyNew(AuraRouter::class));
80
- $di->set(Container\NotFoundDelegateFactory::class, $di->lazyNew(ExpressiveContainer\NotFoundDelegateFactory::class));
81
- $di->set(Delegate\NotFoundDelegate::class, $di->lazyGetCall(ExpressiveContainer\NotFoundDelegateFactory::class, '__invoke', $di));
82
- $di->set('Zend\Expressive\Delegate\DefaultDelegate', $di->lazyGetCall(ExpressiveContainer\NotFoundDelegateFactory::class, '__invoke', $di));
83
- $di->set(Container\ApplicationFactory::class, $di->lazyNew(ExpressiveContainer\ApplicationFactory::class));
84
- $di->set(Application::class, $di->lazyGetCall(ExpressiveContainer\ApplicationFactory::class, '__invoke', $di));
85
-
86
- // Not Found handler
87
- $di->set(Middleware\NotFoundHandler::class, $di->lazyGetCall(ExpressiveContainer\NotFoundHandlerFactory::class, '__invoke', $di));
88
-
89
- // Templating
90
- // In most cases, you can instantiate the template renderer you want to use
91
- // without using a factory:
92
- $di->set(TemplateRendererInterface::class, $di->lazyNew(PlatesRenderer::class));
93
-
94
- // These next two can be added in any environment; they won't be used unless
95
- // you add the WhoopsErrorResponseGenerator as the ErrorResponseGenerator implementation:
96
- $di->set(ExpressiveContainer\WhoopsFactory::class, $di->lazyNew(ExpressiveContainer\WhoopsFactory::class));
97
- $di->set('Zend\Expressive\Whoops', $di->lazyGetCall(ExpressiveContainer\WhoopsFactory::class, '__invoke', $di));
98
- $di->set(ExpressiveContainer\WhoopsPageHandlerFactory::class, $di->lazyNew(ExpressiveContainer\WhoopsPageHandlerFactory::class));
99
- $di->set('Zend\Expressive\WhoopsPageHandler', $di->lazyGetCall(ExpressiveContainer\WhoopsPageHandlerFactory::class, '__invoke', $di));
100
-
101
- // Error Handling
102
- $di->set('Zend\Stratigility\Middleware\ErrorHandler', $di->lazyGetCall(ExpressiveContainer\ErrorHandlerFactory::class, '__invoke', $di));
103
-
104
- // If in development:
105
- $di->set(ExpressiveContainer\WhoopsErrorResponseGeneratorFactory::class, $di->lazyNew(ExpressiveContainer\WhoopsErrorResponseGeneratorFactory::class));
106
- $di->set(Middleware\ErrorResponseGenerator::class, $di->lazyGetCall(ExpressiveContainer\WhoopsErrorResponseGeneratorFactory::class, '__invoke', $di));
107
-
108
- // If in production:
109
- // $di->set(Middleware\ErrorResponseGenerator::class, $di->lazyGetCall(Container\ErrorResponseGeneratorFactory::class, '__invoke', $di));
110
- }
111
-
112
- public function modify(Container $di)
113
- {
114
- /*
115
- $router = $di->get(RouterInterface::class);
116
- $router->addRoute(new Route('/hello/{name}', function ($request, $response, $next) {
117
- $escaper = new Escaper();
118
- $name = $request->getAttribute('name', 'World');
119
- $response->getBody()->write('Hello ' . $escaper->escapeHtml($name));
120
- return $response;
121
- }, Route::HTTP_METHOD_ANY, 'hello'));
122
- */
123
- }
124
- }
125
- ```
39
+ For more information, please see the
40
+ [ zend-auradi-config documentation] ( https://github.com/zendframework/zend-auradi-config/blob/master/README.md ) .
126
41
127
42
Your bootstrap (typically ` public/index.php ` ) will then look like this:
128
43
129
44
``` php
130
45
chdir(dirname(__DIR__));
131
46
require 'vendor/autoload.php';
47
+
132
48
$container = require 'config/container.php';
133
49
$app = $container->get(Zend\Expressive\Application::class);
50
+
134
51
require 'config/pipeline.php';
135
52
require 'config/routes.php';
53
+
136
54
$app->run();
137
55
```
0 commit comments