|
1 | 1 | # PHP-DI integration with Slim
|
2 | 2 |
|
| 3 | +This package configures Slim 3 to work with the [PHP-DI container](http://php-di.org/). |
| 4 | + |
3 | 5 | [](https://travis-ci.org/PHP-DI/Slim-Bridge)
|
4 | 6 |
|
| 7 | +## Why? |
| 8 | + |
| 9 | +### PHP-DI as a container |
| 10 | + |
| 11 | +The most obvious difference with the default Slim installation is that you will be using PHP-DI as the container, which has the following benefits: |
| 12 | + |
| 13 | +- [autowiring](http://php-di.org/doc/autowiring.html) |
| 14 | +- powerful [configuration format](http://php-di.org/doc/php-definitions.html) |
| 15 | +- support for [modular systems](http://php-di.org/doc/definition-overriding.html) |
| 16 | +- ... |
| 17 | + |
| 18 | +If you want to learn more about all that PHP-DI can offer [have a look at its introduction](http://php-di.org/). |
| 19 | + |
| 20 | +### Controllers as services |
| 21 | + |
| 22 | +While you controllers can be simple closures, you can also **write them as classes and have PHP-DI instantiate them only when they are called**: |
| 23 | + |
| 24 | +```php |
| 25 | +class UserController |
| 26 | +{ |
| 27 | + private $userRepository; |
| 28 | + |
| 29 | + public function __construct(UserRepository $userRepository) |
| 30 | + { |
| 31 | + $this->userRepository = $userRepository; |
| 32 | + } |
| 33 | + |
| 34 | + public function delete($request, $response) |
| 35 | + { |
| 36 | + $this->userRepository->remove($request->getAttribute('id')); |
| 37 | + |
| 38 | + $response->getBody()->write('User deleted'); |
| 39 | + return $response; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +$app->delete('/user/{id}', ['UserController', 'delete']); |
| 44 | +``` |
| 45 | + |
| 46 | +Dependencies can then be injected in your controller using [autowiring, PHP-DI config files or even annotations](http://php-di.org/doc/definition.html). |
| 47 | + |
| 48 | +### Controller parameters |
| 49 | + |
| 50 | +By default, Slim controllers have a strict signature: `$request, $response, $args`. The PHP-DI bridge offers a more flexible and developer friendly alternative. |
| 51 | + |
| 52 | +Controller parameters can be any of these things: |
| 53 | + |
| 54 | +- request or response injection (parameters must be named `$request` or `$response`) |
| 55 | +- request attribute injection |
| 56 | +- service injection (by type-hint) |
| 57 | + |
| 58 | +You can mix all these types of parameters together too. They will be matched by priority in the order of the list above. |
| 59 | + |
| 60 | +#### Request or response injection |
| 61 | + |
| 62 | +You can inject the request or response in the controller parameters by name: |
| 63 | + |
| 64 | +```php |
| 65 | +$app->get('/', function (ResponseInterface $response, ServerRequestInterface $request) { |
| 66 | + // ... |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +As you can see, the order of the parameters doesn't matter. That allows to skip injecting the `$request` if it's not needed for example. |
| 71 | + |
| 72 | +#### Request attribute injection |
| 73 | + |
| 74 | +```php |
| 75 | +$app->get('/hello/{name}', function ($name, ResponseInterface $response) { |
| 76 | + $response->getBody()->write('Hello ' . $name); |
| 77 | + return $response; |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +As you can see above, the route's URL contains a `name` placeholder. By simply adding a parameter **with the same name** to the controller, PHP-DI will directly inject it. |
| 82 | + |
| 83 | +#### Service injection |
| 84 | + |
| 85 | +To inject services into your controllers, you can write them as classes. But if you want to write a micro-application using closures, you don't have to give up dependency injection either. |
| 86 | + |
| 87 | +You can inject services by type-hinting them: |
| 88 | + |
| 89 | +```php |
| 90 | +$app->get('/', function (ResponseInterface $response, Twig $twig) { |
| 91 | + return $twig->renderResponse($response, 'home.twig'); |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +> Note: you can only inject services that you can type-hint and that PHP-DI can provide. Type-hint injection is simple, it simply injects the result of `$container->get(/* the type-hinted class */)`. |
| 96 | +
|
5 | 97 | ## Installation
|
6 | 98 |
|
7 | 99 | ```
|
|
0 commit comments