Changed former controller part

This commit is contained in:
Patrick 2014-09-20 19:56:08 +02:00
parent 31b293c49b
commit 26e04d9c0a
4 changed files with 5 additions and 60 deletions

View file

@ -1,4 +1,4 @@
[<< previous](4-http.md) | [next >>](6-controllers.md)
[<< previous](4-http.md) | [next >>](6-dispatching-to-a-class.md)
### Router
@ -76,4 +76,4 @@ $dispatcher = \FastRoute\simpleDispatcher($routeDefinitionCallback);
This is already an improvement, but now all the handler code is in the `Routers.php` file. This is not optimal, so let's fix that in the next part.
[<< previous](4-http.md) | [next >>](6-controllers.md)
[<< previous](4-http.md) | [next >>](6-dispatching-to-a-class.md)

View file

@ -1,55 +0,0 @@
[<< previous](5-router.md) | [next >>](7-inversion-of-control.md)
### Controllers
When I talk about a controller in this tutorial then I am just referring to a class that has handler methods. I am not talking about [MVC (Model-View-Controller)](http://martinfowler.com/eaaCatalog/modelViewController.html) controllers. MVC can't be implemented properly in PHP anyway, at least not in the way it was originally conceived. So forget about MVC and instead let's worry about [separation of concerns](http://en.wikipedia.org/wiki/Separation_of_concerns).
Create a new folder inside the `src/` folder with the name `HelloWorld`. This will be where all your hello world related code will end up in. In there, create `HelloWorldController.php`.
```php
<?php
namespace Example\HelloWorld;
class HelloWorldController
{
public function helloWorld()
{
echo 'Hello World';
}
}
```
The autoloader will only work if the namespace of a class matches the file path and the file name equals the class name. At the beginning I defined `Example` as the root namespace of the application so this is referring to the `src/` folder.
Now let's change the hello world route so that it calls your new controller method instead of the closure. Change your `Routes.php` to this:
```php
return [
['GET', '/hello-world', [
'Example\HelloWorld\HelloWorldController',
'helloWorld',
]],
];
```
Instead of just a callable you are now passing an array. The first value is the fully namespaced controller classname, the second one the method name that you want to call.
To make this work, you will also have to do a small refactor to the routing part of the `Bootstrap.php`:
```php
case \FastRoute\Dispatcher::FOUND:
$className = $routeInfo[1][0];
$method = $routeInfo[1][1];
$vars = $routeInfo[2];
$class = new $className;
$class->$method($vars);
break;
```
So instead of just calling a handler method you are now instantiating the controller object and then calling the method on it.
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-inversion-of-control.md)

View file

@ -1,4 +1,4 @@
[<< previous](6-controllers.md) | [next >>](8-dependency-injector.md)
[<< previous](6-dispatching-to-a-class.md) | [next >>](8-dependency-injector.md)
### Inversion of Control
@ -48,4 +48,4 @@ The `Http\HttpResponse` object implements the `Http\Response` interface, so it f
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)
[<< previous](6-dispatching-to-a-class.md) | [next >>](8-dependency-injector.md)

View file

@ -19,6 +19,6 @@ So let's get started right away with the [first part](1-front-controller.md).
3. [Error Handler](3-error-handler.md)
4. [HTTP](4-http.md)
5. [Router](5-router.md)
6. [Controllers](6-controllers.md)
6. [Dispatching to a Class](6-dispatching-to-a-class.md)
7. [Inversion of Control](7-inversion-of-control.md)
8. [Dependency Injector](8-dependency-injector.md)