finished inversion of control part

This commit is contained in:
Patrick 2014-09-16 21:48:41 +02:00
parent 0427da3caa
commit ae6c24da5d
5 changed files with 60 additions and 8 deletions

View file

@ -1,4 +1,4 @@
[<< previous](5-router.md) | [next >>](7-dependency-injection.md)
[<< previous](5-router.md) | [next >>](7-inversion-of-control.md)
### Controllers
@ -52,4 +52,4 @@ So instead of just calling a handler method you are now instantiating the contro
Now if you visit `http://localhost:8000/hello-world` everything should work. If not, go back and debug. And of course don't forget to commit your changes.
[<< previous](5-router.md) | [next >>](7-dependency-injection.md)
[<< previous](5-router.md) | [next >>](7-inversion-of-control.md)

View file

@ -1,5 +0,0 @@
[<< previous](6-controllers.md)
### Dependency Injection
to be continued...

51
7-inversion-of-control.md Normal file
View file

@ -0,0 +1,51 @@
[<< previous](6-controllers.md) | [next >>](8-dependency-injector.md)
### Inversion of Control
In the last part you have set up a controller and were able to generate output with `echo`. But let's not forget that you have a nice object oriented HTTP abstraction available. But you still need to make it accessible inside the controller class.
The sane option is to use [inversion of control](http://en.wikipedia.org/wiki/Inversion_of_control). This means that instead of giving the class the responsiblity of creating the object it needs, you just ask for them. This is done with [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection).
If it sounds a little complicated right now, don't worry. Just follow the tutorial and once you see how it is implemented things will make more sense.
Change your `HelloWorldController` to the following:
```
<?php
namespace Example\HelloWorld;
use Http\Response;
class HelloWorldController
{
private $response;
public function __construct(Response $response)
{
$this->response = $response;
}
public function hello()
{
$this->response->setContent('Hello World');
}
}
```
Please note that you are [importing](http://php.net/manual/en/language.namespaces.importing.php) `Http\Response` at the top of the file. This means that whenever you use `Response` inside this file, it will resolve to the fully qualified name.
In the contructor you are now explicitly asking for a `Http\Response`. In this case, `Http\Response` is an interface. So any class that implements the interface can be injected. See [type hinting](http://php.net/manual/en/language.oop5.typehinting.php) and [interfaces](php.net/manual/en/language.oop5.interfaces.php) for reference.
Now the code will result in an error because you are not actually injecting anything. So let's fix that in your `Bootstrap.php` where you dispatch when a route was found:
```
$class = new $className($response);
$class->$method($vars);
```
The `Http\HttpResponse` object implements the `Http\Response` interface, so it fulfills the contract and can be used.
Now everything should work again. But if you follow this example, all your controllers will have the same objects injected. This is of course not good, so let's fix that in the next part.
[<< previous](6-controllers.md) | [next >>](8-dependency-injector.md)

5
9-dependency-injector.md Normal file
View file

@ -0,0 +1,5 @@
[<< previous](7-inversion-of-control.md)
### Inversion of Control
to be continued...

View file

@ -20,4 +20,5 @@ So let's get started right away with the [first part](1-front-controller.md).
4. [HTTP](4-http.md)
5. [Router](5-router.md)
6. [Controllers](6-controllers.md)
7. [Dependency Injection](7-dependency-injection.md)
7. [Inversion of Control](7-inversion-of-control.md)
8. [Dependency Injector](8-dependency-injector.md)