From dcdeaa0f7e23134977d0b4e13bb6613d0738d9df Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 14 Sep 2014 13:37:03 +0200 Subject: [PATCH] expanded router part --- 4-http.md | 2 +- 5-router.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/4-http.md b/4-http.md index 6622cb0..17831f1 100644 --- a/4-http.md +++ b/4-http.md @@ -53,7 +53,7 @@ $response->setContent($content); If you want to try a 404 error, use the following code: ``` -$response->setContent('404 - Page not found''); +$response->setContent('404 - Page not found'); $response->setStatusCode(404); ``` diff --git a/5-router.md b/5-router.md index 81f3137..e8ce35c 100644 --- a/5-router.md +++ b/5-router.md @@ -10,4 +10,68 @@ Alternative packages: [symfony/Routing](https://github.com/symfony/Routing), [Au By now you know how to install composer packages, so I will leave that to you. +Now add this code block to your `Bootstrap.php` file where you added the hello world message in the last part. + +``` +$dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) { + $r->addRoute('GET', '/hello-world', function () { + echo 'Hello World'; + }); + $r->addRoute('GET', '/another-route', function () { + echo 'This works too'; + }); +}); + +$routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri()); +switch ($routeInfo[0]) { + case \FastRoute\Dispatcher::NOT_FOUND: + $response->setContent('404 - Page not found'); + $response->setStatusCode(404); + break; + case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED: + $response->setContent('405 - Method not allowed'); + $response->setStatusCode(405); + break; + case \FastRoute\Dispatcher::FOUND: + $handler = $routeInfo[1]; + $vars = $routeInfo[2]; + call_user_func($handler, $vars); + break; +} +``` + +In the first part of the code, you are registering the available routes for you application. In the second part, the dispatcher gets called and the appropriate part of the switch statement will be executed. If a route was found, the handler callable will be executed. + +This setup might work for really small applications, but once you start adding a few routes your bootstrap file will quickly get cluttered. So let's move them out into a separate file. + +Create a `Routes.php` file in the `src/` folder. It should look like this: + +``` +addRoute($route[0], $route[1], $route[2]); + } +}; + +$dispatcher = \FastRoute\simpleDispatcher($routeDefinitionCallback); +``` + + + to be continued... \ No newline at end of file