omegacode/jwt-secured-api-twig

This package integrates the template engine twig into the api frame work.

1.0.1 2021-01-04 22:34 UTC

This package is auto-updated.

Last update: 2024-04-05 05:29:05 UTC


README

alt text

JWT secured API - Twig

This package integrates the template engine twig into the api frame work.

Use twig in actions

This package allows you to create actions with twig rendering out of the box. You only have your action class extending \OmegaCode\JwtSecuredApiTwig\Action\AbstractTwigAction.

An example could be:

namespace Vendor\MyProject\Action;

use OmegaCode\JwtSecuredApiTwig\Action\AbstractTwigAction;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class IndexAction extends AbstractTwigAction
{
    public function __invoke(Request $request, Response $response): Response
    {
        return $this->render($response, [
            'currentTime' => time(),
        ]);
    }

    protected function getTemplateFilePath(): string
    {
        return 'index.html'; // res/templates/index.html
    }
}

Use twig to render

To use twig to render a template file, you can simply inject the service Twig\Environment. An example on how to use the service can be found in class \OmegaCode\JwtSecuredApiTwig\Action\AbstractTwigAction.

Manipulate template directories

Maybe you want to change the default loading directory for twig templates (res/templates). This can be archived by creating a subscriber like below.

services:
  Vendor\MyProject\Subscriber\TwigTemplateSubscriber:
    tags:
      - 'kernel.event_subscriber'
namespace Vendor\MyProject\Subscriber;

use OmegaCode\JwtSecuredApiTwig\Event\Twig\CollectTemplatesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class TwigTemplateSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            CollectTemplatesEvent::NAME => 'onCollectTemplates',
        ];
    }

    public function onCollectTemplates(CollectTemplatesEvent $event): void
    {
        $event->addTemplatePath(APP_ROOT_PATH . '/my/custom/templates');
    }
}