Skip to content

Latest commit

 

History

History
88 lines (62 loc) · 2.65 KB

responders.md

File metadata and controls

88 lines (62 loc) · 2.65 KB

Responders

Responders respond data: transform data to a Symfony response, return a success in a CLI operation.

Default responders

When your operation is an instance of Sylius\Component\Resource\Metadata\HttpOperation two responders are configured by default.

The responder will automatically choose the responder depending on the request format:

Request format Responder
html Sylius\Component\Resource\Symfony\Request\State\TwigResponder
json Sylius\Component\Resource\Symfony\Request\State\ApiResponder
xml Sylius\Component\Resource\Doctrine\Common\State\ApiResponder

Twig Responder

The Twig responder is used to render data into a Symfony response. It's used for HTML responses.

The variables that are passed to the Twig templates depends on the operation (See Configure your operations chapter).

Customize Twig template variables

Some variables are already available on your operations, but you can add more variables easily.

As an example, we add a foo variable to the Twig template with bar as value.

// src/Twig/Context/Factory/ShowSubscriptionContextFactory.php

namespace App\Twig\Context\Factory;

use Sylius\Resource\Context\Context;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\Twig\Context\Factory\ContextFactoryInterface;

final class ShowSubscriptionContextFactory implements ContextFactoryInterface
{
    public function __construct(private ContextFactoryInterface $decorated)
    {
    }

    public function create(mixed $data, Operation $operation, Context $context): array
    {
        return array_merge($this->decorated->create($data, $operation, $context), [
            'foo' => 'bar',
        ]);
    }
}

Use it on your operation.

// src/Entity/Subscription.php

namespace App\Entity;

use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Model\ResourceInterface;

#[AsResource]
#[Show(
    template: 'subscription/show.html.twig',
    twigContextFactory: ShowSubscriptionContextFactory::class,
)]
class Subscription implements ResourceInterface
{
}

API Responder

The API responder is used to render serialized data into a Symfony response. It's used for JSON/XML responses.

Go back to the documentation's index