1. Go to this page and download the library: Download wizzaq/rest-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wizzaq / rest-bundle example snippets
namespace App\Controller;
use App\Form\Filter\MyEntityFilterType;
use App\Form\Dto\Filter\MyEntityFilter;
use App\Repository\MyEntityRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Wizzaq\RestBundle\Attribute\ProcessForm;
class MyEntityController extends AbstractController
{
#[Route('/my-entity', methods: 'GET')]
public function list(
#[ProcessForm(MyEntityFilterType::class, mapEntity: false, throwOnNotValid: false)] ?MyEntityFilter $filter = null,
MyEntityRepository $repository
): Response {
return $this->json(['list' => $repository->list($filter ?? new MyEntityFilter())]);
}
#[Route('/my-entity', methods: 'POST')]
#[Route('/my-entity/{id}', methods: 'PUT')]
public function edit(
#[ProcessForm(MyEntityType::class)] MyEntity $entity,
EntityManagerInterface $em
): Response {
// $entity is processed by form and validated
// Wizzaq\RestBundle\Exception\FormValidationException will be thrown if validation failed
$em->persist($entity);
$em->flush();
return new JsonResponse(['id' => $entity->getId()]);
}
}
> #[Route('/my-entity/{id}', methods: 'PUT')]
> public function edit(
> #[ProcessForm(form: MyEntityType::class)] MyEntity $entity,
> EntityManagerInterface $em
> ): Response {
> // $entity is processed by form and validated
> // Wizzaq\RestBundle\Exception\FormValidationException will be thrown if validation failed
> $em->persist($entity);
> $em->flush();
>
> return new JsonResponse(['id' => $entity->getId()]);
> }
>
> #[Route('/my-entity/{id}', methods: 'PUT')]
> public function edit(
> #[ProcessForm(form: MyEntityType::class, mapEntity: true)] MyEntity $entity,
> EntityManagerInterface $em
> ): Response {
> // $entity is processed by form and validated
> // Wizzaq\RestBundle\Exception\FormValidationException will be thrown if validation failed
> $em->persist($entity);
> $em->flush();
>
> return new JsonResponse(['id' => $entity->getId()]);
> }
>