Symfony 4.2 and API Platform: how to use custom operations before return a response : <p>I'm trying to create a custom operation when calling /api/employees.</p>

<p>I have done a method in EmployeesController.php called jiraSync, this method must be called before returning the response in JSON to obtain a synchronized response.</p>

<p>I have also thought about creating a service, but I do not know if it will be a good idea, what do you recommend?</p>

<p>This is my Employees <strong>entity</strong>:</p>

<pre><code>&lt;?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\EmployeeController;
use Doctrine\ORM\Mapping as ORM;

/**
 * Employees
 *
 * @ApiResource()
 * @ORM\Table(name="employees", uniqueConstraints={@ORM\UniqueConstraint(name="email_UNIQUE", columns={"email"})}, indexes={@ORM\Index(name="fk_employees_departments_idx", columns={"department_id"})})
 * @ORM\Entity
 */
class Employees
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=false)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=50, nullable=false)
     */
    private $name;

    /**
     * @var string|null
     *
     * @ORM\Column(name="surname", type="string", length=100, nullable=true)
     */
    private $surname;

    /**
     * @var string|null
     *
     * @ORM\Column(name="image", type="string", length=255, nullable=true)
     */
    private $image;

    /**
     * @var \Departments
     *
     * @ORM\ManyToOne(targetEntity="Departments")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="department_id", referencedColumnName="id")
     * })
     */
    private $department;

    /**
     * @var bool
     *
     * @ORM\Column(name="is_enabled", type="boolean", nullable=false)
     */
    private $isEnabled = '0';

    public function getId(): ?int
    {
        return $this-&gt;id;
    }

    public function getEmail(): ?string
    {
        return $this-&gt;email;
    }

    public function setEmail(string $email): self
    {
        $this-&gt;email = $email;

        return $this;
    }

    public function getName(): ?string
    {
        return $this-&gt;name;
    }

    public function setName(string $name): self
    {
        $this-&gt;name = $name;

        return $this;
    }

    public function getSurname(): ?string
    {
        return $this-&gt;surname;
    }

    public function setSurname(?string $surname): self
    {
        $this-&gt;surname = $surname;

        return $this;
    }

    public function getImage(): ?string
    {
        return $this-&gt;image;
    }

    public function setImage(?string $image): self
    {
        $this-&gt;image = $image;

        return $this;
    }

    public function getDepartment(): ?Departments
    {
        return $this-&gt;department;
    }

    public function setDepartment(?Departments $department): self
    {
        $this-&gt;department = $department;

        return $this;
    }

    public function getIsEnabled(): ?bool
    {
        return $this-&gt;isEnabled;
    }

    public function setIsEnabled($isEnabled): self
    {
        $this-&gt;isEnabled = $isEnabled;

        return $this;
    }


}
</code></pre>

<p>And this is my <strong>controller</strong>:</p>

<pre><code>&lt;?php

namespace App\Controller;

use App\Entity\Employees;
use JiraRestApi\JiraException;
use JiraRestApi\User\UserService;
use Symfony\Bridge\Doctrine\Tests\Fixtures\Employee;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class EmployeeController extends AbstractController
{
    public function jiraSync()
    {
        // ...
    }
}
</code></pre>
