diff --git a/6-dispatching-to-a-class.md b/6-dispatching-to-a-class.md index 31f2fbe..52c96e9 100644 --- a/6-dispatching-to-a-class.md +++ b/6-dispatching-to-a-class.md @@ -2,20 +2,22 @@ ### Dispatching to a Class -In this tutorial we won't implement [MVC (Model-View-Controller)](http://martinfowler.com/eaaCatalog/modelViewController.html). 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). +In this tutorial we won't implement [MVC (Model-View-Controller)](http://martinfowler.com/eaaCatalog/modelViewController.html). MVC can't be implemented properly in PHP anyway, at least not in the way it was originally conceived. If you want to learn more about this, read [A Beginner's Guide To MVC](http://blog.ircmaxell.com/2014/11/a-beginners-guide-to-mvc-for-web.html) and the followup posts. -Instead of just calling everything a controller, let's give our names descriptive names that describe what the class actually does. In this case, we will just display content, so a fitting name would be `Presenter`. If the class does something else, we will name it accordingly. +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 `HelloWorldPresenter.php`. +We will need a descriptive name for the classes that handle the requests. For this tutorial I will use `Controllers` because that will be familiar for the people coming from a framework background. You could also name them `Handlers`. + +Create a new folder inside the `src/` folder with the name `Controllers`.In this folder we will place all our controller classes. In there, create a `Homepage.php` file. ```php >](7-inversion-of-control.md) \ No newline at end of file diff --git a/7-inversion-of-control.md b/7-inversion-of-control.md index ecfec07..1192545 100644 --- a/7-inversion-of-control.md +++ b/7-inversion-of-control.md @@ -2,22 +2,22 @@ ### Inversion of Control -In the last part you have set up a presenter class and generated output with `echo`. But let's not forget that you have a nice object oriented HTTP abstraction available. But right now it's not accessible inside your class. +In the last part you have set up a controller class and generated output with `echo`. But let's not forget that you have a nice object oriented HTTP abstraction available. But right now it's not accessible inside your 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 `HelloWorldPresenter` to the following: +Change your `Homepage` controller to the following: ```php response = $response; } - public function hello() + public function show() { $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. +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](http://php.net/manual/en/language.oop5.interfaces.php) for reference. diff --git a/8-dependency-injector.md b/8-dependency-injector.md index d48ee35..f4939af 100644 --- a/8-dependency-injector.md +++ b/8-dependency-injector.md @@ -60,17 +60,17 @@ $class->$method($vars); Now all your controller constructor dependencies will be automatically resolved with Auryn. -Go back to your `HelloWorldPresenter.php` and change it to the following: +Go back to your `Homepage` controller and change it to the following: ```php response = $response; } - public function hello() + public function show() { $content = '

Hello World

'; $content .= 'Hello ' . $this->request->getParameter('name', 'stranger'); @@ -90,7 +90,7 @@ class HelloWorldPresenter } ``` -As you can see now the class has two dependencies. Try to access the page with a GET parameter like this `http://localhost:8000/hello-world?name=Arthur%20Dent`. +As you can see now the class has two dependencies. Try to access the page with a GET parameter like this `http://localhost:8000/?name=Arthur%20Dent`. Congratulations, you have now successfully laid the groundwork for your application. diff --git a/9-templating.md b/9-templating.md index 29f143c..50ccb93 100644 --- a/9-templating.md +++ b/9-templating.md @@ -68,18 +68,18 @@ Of course we also have to add a definition in our `Dependencies.php` file becaus `$injector->alias('Example\Template\Engine', 'Example\Template\MustacheEngineAdapter');` -Now in your `HelloWorldPresenter`, add the new dependency like this: +Now in your `Homepage` controller, add the new dependency like this: ```php $this->request->getParameter('name', 'stranger'), @@ -125,14 +125,14 @@ $injector->define('Mustache_Engine', [ ]); ``` -In your project root folder, create a `templates` folder. In there, create a folder `HelloWorld` and in there a file `Hello.mustache`. The content of the file should look like this: +In your project root folder, create a `templates` folder. In there, create a file `Homepage.mustache`. The content of the file should look like this: ```

Hello World

Hello {{ name }} ``` -Now you can go back to your `HelloWorldPresenter` and change the render line to `$content = $this->templateEngine->render('HelloWorld/Hello', $data);` +Now you can go back to your `Homepage` controller and change the render line to `$content = $this->templateEngine->render('Homepage', $data);` Navigate to the hello page in your browser to make sure everything works. And as always, don't forget to commit your changes.