Update 05-router.md
This commit is contained in:
parent
4169afd542
commit
b2d2fee013
1 changed files with 5 additions and 3 deletions
|
@ -12,7 +12,7 @@ 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.
|
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.
|
Now add this code block to your `Bootstrap.php` file where you added the 'hello world' message in the last chapter.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
|
$dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
|
||||||
|
@ -49,7 +49,7 @@ This setup might work for really small applications, but once you start adding a
|
||||||
Create a `Routes.php` file in the `src/` folder. It should look like this:
|
Create a `Routes.php` file in the `src/` folder. It should look like this:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php declare(strict_types = 1);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
['GET', '/hello-world', function () {
|
['GET', '/hello-world', function () {
|
||||||
|
@ -61,7 +61,7 @@ return [
|
||||||
];
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
Now let's rewrite the route collection part to use the `Routes.php` file.
|
Now let's rewrite the route dispatcher part to use the `Routes.php` file.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$routeDefinitionCallback = function (\FastRoute\RouteCollector $r) {
|
$routeDefinitionCallback = function (\FastRoute\RouteCollector $r) {
|
||||||
|
@ -76,4 +76,6 @@ $dispatcher = \FastRoute\simpleDispatcher($routeDefinitionCallback);
|
||||||
|
|
||||||
This is already an improvement, but now all the handler code is in the `Routes.php` file. This is not optimal, so let's fix that in the next part.
|
This is already an improvement, but now all the handler code is in the `Routes.php` file. This is not optimal, so let's fix that in the next part.
|
||||||
|
|
||||||
|
Don't forget to commit your changes at the end of each chapter.
|
||||||
|
|
||||||
[<< previous](04-http.md) | [next >>](06-dispatching-to-a-class.md)
|
[<< previous](04-http.md) | [next >>](06-dispatching-to-a-class.md)
|
||||||
|
|
Loading…
Reference in a new issue