You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rewrite exercise to use league/route and symfony/string (#106)
* Rewrite exercise to use league/route and symfony/string
* Update failure class
* Delete & ignore lock files
* Don't store solution composer.lock files
Copy file name to clipboardExpand all lines: exercises/dependency-heaven/problem/problem.md
+61-7Lines changed: 61 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
Write an HTTP **server** that serves JSON data when it receives a POST request to `/reverse`, `/swapcase` and `/titleize`.
1
+
Write an HTTP **server** that serves JSON data when it receives a POST request to `/reverse`, `/snake` and `/titleize`.
2
2
3
3
The POST data will contain a single parameter `data` which you will need to manipulate depending on the endpoint.
4
4
@@ -12,13 +12,13 @@ A request with `data = "PHP School is awesome!"` should return the response:
12
12
}
13
13
```
14
14
15
-
### /swapcase
15
+
### /snake
16
16
17
17
A request with `data = "No, It Really Is..."` should return the response:
18
18
19
19
```json
20
20
{
21
-
"result": "nO, iT rEALLY iS..."
21
+
"result": "no_it_really_is"
22
22
}
23
23
```
24
24
@@ -32,9 +32,61 @@ A request with `data = "you know you love it, don't you?"` should return the res
32
32
}
33
33
```
34
34
35
-
You should use the routing library `klein/klein` for this task pulling it in as a dependency through Composer.
35
+
You should use the routing library `league/route` for this task, pulling it in as a dependency through Composer. `league/route` allows us to define actions which respond to requests based on the request URI and HTTP method.
36
36
37
-
You will also be required to use `danielstjules/stringy` to manipulate the data as this correctly handles multibyte characters.
37
+
The library works by accepting a PSR7 request and returns to you a PSR7 response. It is up to you to marshal the request object and output the response to the browser.
38
+
39
+
There are a few other components we need, in order to use `league/route`:
40
+
41
+
***laminas/laminas-diactoros** - For the PSR7 requests and responses.
42
+
***laminas/laminas-httphandlerrunner** - For outputting the PSR7 response to the browser.
43
+
44
+
`laminas/laminas-diactoros` is a PSR7 implementation. PSR's are standards defined by the PHP-FIG, a committee of PHP projects, attempting to increase interoperability in the PHP ecosystem. PSR7 is a standard for modelling HTTP requests. We can use the `laminas/laminas-diactoros` package to marshal a PSR7 request object from the PHP super globals like so:
`$request` is now a PSR7 request, that can be used with `league/route`.
51
+
52
+
`laminas/laminas-httphandlerrunner` provides a simple method to output the PSR7 response to the browser, handling headers, status codes and the content. Use it like so:
53
+
54
+
```php
55
+
(new \Laminas\HttpHandlerRunner\Emitter\SapiEmitter())->emit($response);
56
+
```
57
+
58
+
Where `$response` is a PSR7 response.
59
+
60
+
In between this, you will need to define your routes and execute the dispatching mechanism to receive a response. Refer to the `league\route`[documentation](https://route.thephpleague.com/5.x/usage/).
61
+
62
+
Each route action will be passed the PSR7 request where you can access the request parameters and body. To access the `data` key from the request body, you would use the following:
63
+
64
+
```php
65
+
$data = $request->getParsedBody()['data'] ?? '';
66
+
```
67
+
68
+
In each action, you are expected to return a PSR7 response. `laminas/laminas-diactoros` provides a few ways to accomplish this:
69
+
70
+
A text response:
71
+
72
+
```php
73
+
$response = new \Laminas\Diactoros\Response('My Content');
74
+
```
75
+
76
+
A JSON response:
77
+
78
+
```php
79
+
$response = (new \Laminas\Diactoros\Response(json_encode(['desert' => 'cookies'])))
0 commit comments