Skip to content

Commit c5baf33

Browse files
authored
Merge branch 'main' into issue-110
Signed-off-by: Howriq <[email protected]>
2 parents 5513de2 + d63fbf4 commit c5baf33

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

docs/book/v5/tutorials/create-book-module.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,13 @@ class BookHandler extends AbstractHandler implements RequestHandlerInterface
560560
return $this->createResponse($request, $book);
561561
}
562562

563+
public function getCollection(ServerRequestInterface $request): ResponseInterface
564+
{
565+
$books = $this->bookService->getRepository()->getBooks($request->getQueryParams());
566+
567+
return $this->createResponse($request, $books);
568+
}
569+
563570
public function post(ServerRequestInterface $request): ResponseInterface
564571
{
565572
$inputFilter = (new BookInputFilter())->setData($request->getParsedBody());
@@ -654,7 +661,7 @@ class RoutesDelegator
654661

655662
$app->get(
656663
'/book/' . $uuid,
657-
BookHandler::class,
664+
BookCollection::class,
658665
'book.show'
659666
);
660667

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Rendering and sending emails
2+
3+
In the previous versions of Dotkernel API we have been composing email bodies using **Twig** from the `mezzio/mezzio-twigrenderer` package.
4+
In the current version of Dotkernel API, we introduced the core mail service `Core/src/App/src/Service/MailService` which is responsible for sending all emails.
5+
6+
Being a core service, `MailService` is used across all projects implementing the Core architecture.
7+
To compose and send an email, a solid implementation of `TemplateRendererInterface` was required to be injected into `MailService`, because each method rendered and parsed their respective templates in place before sending an email.
8+
This is acceptable with other Dotkernel applications which in most cases return a rendered template, but being that Dotkernel API mostly returns JSON objects, rendered with a different renderer, **Twig** had to be replaced with a lighter solution.
9+
10+
The solution is a custom [`Api\App\Template\Renderer`](https://github.com/dotkernel/api/blob/6.0/src/App/src/Template/Renderer.php) implementing [`Api\App\Template\RendererInterface`](https://github.com/dotkernel/api/blob/6.0/src/App/src/Template/RendererInterface.php).
11+
This is a lightweight renderer, aimed at rendering a combination of **PHP** and **HTML** files with `phtml` extension.
12+
13+
With the new solution, `MailService` requires no implementation of any renderer because it no longer has to render templates internally.
14+
Instead, an implementation of `Api\App\Template\RendererInterface` is first injected in the handler:
15+
16+
```php
17+
class ExampleHandler extends AbstractHandler
18+
{
19+
#[Inject(
20+
MailService::class,
21+
RendererInterface::class,
22+
)]
23+
public function __construct(
24+
protected MailService $mailService,
25+
protected RendererInterface $renderer,
26+
) {
27+
}
28+
```
29+
30+
Then, the handler calls the renderer and saves the rendered template in a variable:
31+
32+
```php
33+
$body = $this->renderer->render('user::welcome', ['user' => $user]);
34+
```
35+
36+
And finally, the handler calls the mail service with the composed $body being passed as a parameter to the method which sends the email:
37+
38+
```php
39+
// $user object contains email, firstname and lastname
40+
41+
$this->mailService->sendWelcomeMail($user, $body);
42+
```
43+
44+
> Other Dotkernel applications implementing the Core architecture do the same in the handlers, but keep using Twig as the template renderer.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ nav:
3737
- "Exceptions": v6/core-features/exceptions.md
3838
- "Dependency Injection": v6/core-features/dependency-injection.md
3939
- "Error reporting": v6/core-features/error-reporting.md
40+
- "Rendering and Sending emails": v6/core-features/rendering-and-sending-emails.md
4041
- Extended features:
4142
- "Core and App": v6/extended-features/core-and-app.md
4243
- "New Handler Structure": v6/extended-features/handler-structure.md

0 commit comments

Comments
 (0)