Skip to content

Commit 82345b6

Browse files
authored
Merge pull request #106 from Howriq/issue-97
Issue #97: route grouping
2 parents 453af19 + a603b26 commit 82345b6

File tree

4 files changed

+110
-3
lines changed

4 files changed

+110
-3
lines changed

docs/book/v6/extended-features/handler-structure.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ In this way, the developer can easily figure out the functionality of each handl
3838

3939
## Mapping of the handlers
4040

41-
In the picture below you can see the mapping of our current handlers with their respective paths and actions:
42-
43-
![Dotkernel API Mapping!](https://docs.dotkernel.org/img/api/naming-convention.png)
41+
The full mapping of the handlers and their current paths and actions can be found [here](https://docs.dotkernel.org/img/api/naming-convention.png).
42+
![naming-convention-thumbnail](https://docs.dotkernel.org/img/api/naming-convention-thumbnail.png)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Problem details
2+
3+
With the usage of `mezzio/mezzio-problem-details` we have implemented a way to help the developers understand better the errors that they are getting from their APIs based on the [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html) standards.
4+
5+
Example of a response with details:
6+
7+
```json
8+
{
9+
"title": "Unauthorized",
10+
"type": "https://docs.dotkernel.org/api-documentation/v5/core-features/error-reporting/",
11+
"status": 401,
12+
"detail": "You are not allowed to report errors."
13+
}
14+
```
15+
16+
Usually the response includes:
17+
18+
- A title related to the error
19+
- The type of error
20+
- The status of the request (e.g `404`)
21+
- Different error messages
22+
23+
More fields can be added based on the preference of the developer.
24+
25+
## Our changes
26+
27+
In order for us to implement this new feature, a new middleware component was required.
28+
We have created `ProblemDetailsMiddleware` along with `ProblemDetailsNotFoundHandler` which is being called in the `config/pipeline.php` file.
29+
Our exceptions have also been modified in order to be slimmed around the requirement for the `problem-details` package.
30+
31+
Example from `src/App/src/Exception/BadRequestException.php`:
32+
33+
```php
34+
public static function create(string $detail, string $type = '', string $title = '', array $additional = []): self
35+
{
36+
$exception = new self();
37+
38+
$exception->type = $type;
39+
$exception->detail = $detail;
40+
$exception->status = StatusCodeInterface::STATUS_BAD_REQUEST;
41+
$exception->title = $title;
42+
$exception->additional = $additional;
43+
44+
return $exception;
45+
}
46+
```
47+
48+
An example configuration file for setting custom links has also been created in `config/autoload/problem-details.global.php`.
49+
Here the statuses of the API calls are being attributed to a link.
50+
51+
```php
52+
return [
53+
'problem-details' => [
54+
'default_types_map' => [
55+
StatusCodeInterface::STATUS_BAD_REQUEST
56+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-400-bad-request',
57+
StatusCodeInterface::STATUS_UNAUTHORIZED
58+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-401-unauthorized',
59+
StatusCodeInterface::STATUS_FORBIDDEN
60+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-403-forbidden',
61+
StatusCodeInterface::STATUS_NOT_FOUND
62+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-404-not-found',
63+
StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED
64+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-405-method-not-allowed',
65+
StatusCodeInterface::STATUS_NOT_ACCEPTABLE
66+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-406-not-acceptable',
67+
StatusCodeInterface::STATUS_CONFLICT
68+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-409-conflict',
69+
StatusCodeInterface::STATUS_GONE
70+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-410-gone',
71+
StatusCodeInterface::STATUS_UNSUPPORTED_MEDIA_TYPE
72+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-415-unsupported-media-type',
73+
StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR
74+
=> 'https://datatracker.ietf.org/doc/html/rfc9110#name-500-internal-server-error',
75+
],
76+
],
77+
];
78+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Route grouping
2+
3+
In Dotkernel 6.0 with the help of the new [dot-router](https://docs.dotkernel.org/dot-router/v1/overview/) package, we have managed to implement a nicer way of creating routes.
4+
A lot of the times developers need to create sets of routes that have a similar format. As an example:
5+
6+
```php
7+
$app->post('/product/create', CreateProductHandler::class, 'product:create');
8+
$app->delete('/product/delete/{id}', DeleteProductHandler::class, 'product:delete');
9+
$app->patch('/product/update/{id}', UpdateProductHandler::class, 'product:update');
10+
$app->get('/product/view/{id}', GetProductHandler::class, 'product:view');
11+
```
12+
13+
Along with the features from `mezzio/mezzio-fastroute`, the new `dot-router` package provides the ability to create route groups which are collections of routes that have the same base string for the path.
14+
15+
Here we have an example from `src/User/src/RoutesDelegator.php` with the new grouping method:
16+
17+
```php
18+
$routeCollector->group('/user/' . $uuid)
19+
->delete('', DeleteUserResourceHandler::class, 'user::delete-user')
20+
->get('', GetUserResourceHandler::class, 'user::view-user')
21+
->patch('', PatchUserResourceHandler::class, 'user::update-user');
22+
```
23+
24+
The advantages of this new implementation:
25+
26+
- DRY - no need for repeating common route parts
27+
- encapsulation - similar routes are grouped in a single block of code (vs each route a separate statement)
28+
- easy path refactoring - modify all routes at once by changing only the prefix
29+
- easy copying/moving - copying/moving an entire group makes sure that you don't accidentally omit a route

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ nav:
4040
- Extended features:
4141
- "Core and App": v6/extended-features/core-and-app.md
4242
- "New Handler Structure": v6/extended-features/handler-structure.md
43+
- "Route Grouping": v6/extended-features/route-grouping.md
4344
- Commands:
4445
- "Create admin account": v6/commands/create-admin-account.md
4546
- "Generate database migrations": v6/commands/generate-database-migrations.md

0 commit comments

Comments
 (0)