Permalink
Browse files

Update the documentation following Slim-Bridge 1.1.0

Apply the changes of this pull request: PHP-DI/Slim-Bridge#29
  • Loading branch information...
mnapoli committed Nov 14, 2017
1 parent 38b35cd commit 6cb173ab9beb6c11463756b675d8033a11e422ec
Showing with 21 additions and 4 deletions.
  1. +21 −4 doc/frameworks/slim.md
View
@@ -46,9 +46,10 @@ By default, Slim controllers have a strict signature: `$request, $response, $arg
Controller parameters can be any of these things:
- request or response injection (parameters must be named `$request` or `$response`)
- request attribute injection
- service injection (by type-hint)
- the request or response (parameters must be named `$request` or `$response`)
- route placeholders
- request attributes
- services (injected by type-hint)
You can mix all these types of parameters together too. They will be matched by priority in the order of the list above.
@@ -64,7 +65,7 @@ $app->get('/', function (ResponseInterface $response, ServerRequestInterface $re
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.
#### Request attribute injection
#### Route placeholder injection
```php
$app->get('/hello/{name}', function ($name, ResponseInterface $response) {
@@ -75,6 +76,22 @@ $app->get('/hello/{name}', function ($name, ResponseInterface $response) {
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.
#### Request attribute injection
```php
$app->add(function ($request, $response, $next) {
$request = $request->withAttribute('name', 'Bob');
return $next($request, $response);
});
$app->get('/', function ($name, ResponseInterface $response) {
$response->getBody()->write('Hello ' . $name);
return $response;
});
```
As you can see above, a middleware sets a `name` attribute. By simply adding a parameter **with the same name** to the controller, PHP-DI will directly inject it.
#### Service injection
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.

0 comments on commit 6cb173a

Please sign in to comment.