Skip to content

Commit e3ba9a3

Browse files
committed
Add documentation
1 parent 835c60c commit e3ba9a3

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,99 @@
11
# PHP-DI integration with Slim
22

3+
This package configures Slim 3 to work with the [PHP-DI container](http://php-di.org/).
4+
35
[![Build Status](https://travis-ci.org/PHP-DI/Slim-Bridge.svg?branch=master)](https://travis-ci.org/PHP-DI/Slim-Bridge)
46

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+
597
## Installation
698

799
```

src/ControllerInvoker.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ public function __invoke(
3535
ResponseInterface $response,
3636
array $routeArguments
3737
) {
38+
// Inject the request and response by parameter name
3839
$parameters = [
3940
'request' => $request,
4041
'response' => $response,
4142
];
43+
44+
// Inject the route arguments by name
4245
$parameters += $routeArguments;
4346

4447
return $this->invoker->call($callable, $parameters);

src/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
->constructor(get('foundHandler.invoker')),
5757
'foundHandler.invoker' => function (ContainerInterface $c) {
5858
$resolvers = [
59+
// Inject parameters by name first
5960
new AssociativeArrayResolver,
61+
// Then inject services by type-hints for those that weren't resolved
6062
new TypeHintContainerResolver($c),
6163
];
6264
return new Invoker(new ResolverChain($resolvers), $c);

0 commit comments

Comments
 (0)