Single Action/Invocable/Framework Agnostics Presenters in Nette

Tomáš Votruba
Moderator | 1114
+
+2
-

On 87th Posobota I've talked about the thinnest form of Nette Presenters see video.

How does Single Action/Invocable/Framework Agnostics Presenter look like?

For JSON presenter it looks like this:

namespace App\Presenter;

use Nette\Application\Responses\JsonResponse;

final class ApiPresenter
{
    public function __invoke(): JsonResponse
    {
        return new JsonResponse('Hi!');
    }
}

For classic template presenter like this:

namespace App\Presenter;

use Symplify\SymbioticController\Adapter\Nette\Template\TemplateRenderer;

final class StandalonePresenter
{
    /**
     * @var TemplateRenderer
     */
    private $templateRenderer;

    public function __construct(TemplateRenderer $templateRenderer)
    {
        $this->templateRenderer = $templateRenderer;
    }

    public function __invoke(): string
    {
        return $this->templateRenderer->renderFileWithParameters(
            __DIR__ . '/templates/Contact.latte'
        );
    }
}

Since the talk I have:

Huge thanks @JanMikeš for feedback, first user and review.