added to the http part

This commit is contained in:
Patrick 2014-09-13 00:15:04 +02:00
parent c47470140f
commit e65d23fd5c
2 changed files with 46 additions and 3 deletions

View file

@ -1,4 +1,4 @@
[<< previous](3-error-handler.md) [<< previous](3-error-handler.md) | [next >>](5-router.md)
### HTTP ### HTTP
@ -6,7 +6,7 @@ PHP already has a few things built in to make working with HTTP easier. For exam
These are good if you just want to get a quick and dirty script runnin. But if you want to write clean, maintanable, [SOLID](http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29) code, then you will want a class with a nice object oriented interface that you can use in your application. These are good if you just want to get a quick and dirty script runnin. But if you want to write clean, maintanable, [SOLID](http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29) code, then you will want a class with a nice object oriented interface that you can use in your application.
Once again, you don't have to reinvent the wheel and just install a package. I decided to write my own [HTTP component](https://github.com/PatrickLouys/http). Once again, you don't have to reinvent the wheel and just install a package. I decided to write my own [HTTP component](https://github.com/PatrickLouys/http) because I did not like the existing components, but you don't have to do the same.
Some alternatives: [Symfony HttpFoundation](https://github.com/symfony/HttpFoundation), [Nette HTTP Component](https://github.com/nette/http), [Aura Http](https://github.com/auraphp/Aura.Http), [sabre/http](https://github.com/fruux/sabre-http) Some alternatives: [Symfony HttpFoundation](https://github.com/symfony/HttpFoundation), [Nette HTTP Component](https://github.com/nette/http), [Aura Http](https://github.com/auraphp/Aura.Http), [sabre/http](https://github.com/fruux/sabre-http)
@ -22,5 +22,43 @@ Again, edit the `composer.json` to add the new component and then run `composer
}, },
``` ```
Now you can add the following below your error handler code in your `Bootstrap.php` (and don't forget to remove the exception):
to be continued... ```
$request = new \Http\HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
$response = new \Http\HttpResponse;
```
This sets up the `Request` and `Response` objects that you can use in your other classes to get request data and send a response back to the browser.
To actually send something back, you will also need to add the following snippet at the end of your `Bootstrap.php` file:
```
foreach ($response->getHeaders() as $header) {
header($header);
}
echo $response->getContent();
```
This will send the response data to the browser. If you don't do this, nothing happens as the `Response` object only stores data. This is handled differently by most other HTTP components where the classes send data back to the browser as a side-effect, so keep that in mind if you use another component.
Right now it is just sending an empty response back to the browser with the status code `200`, so to change that add the following code between the code snippets from above:
```
$content = '<h1>Hello World</h1>';
$response->setContent($content);
```
If you want to try a 404 error, use the following code:
```
$response->setContent('404 - Page not found'');
$response->setStatusCode(404);
```
Remember that the object is only storing data, so you if you set multiple status codes before you send the response, the last one will be applied.
I will show you in later parts how to use the different features of the components. In the meantime, feel free to read the [documentation](https://github.com/PatrickLouys/http) or the source code if you want to find out how something works.
[<< previous](3-error-handler.md) | [next >>](5-router.md)

5
5-router.md Normal file
View file

@ -0,0 +1,5 @@
[<< previous](4-http.md)
### Router
to be continued...