PHP code example of azyouness / request-to-form-bundle
1. Go to this page and download the library: Download azyouness/request-to-form-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/ */
azyouness / request-to-form-bundle example snippets
use App\Entity\Post;
use AzYouness\RequestToFormBundle\Attribute\MapRequestToForm;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/posts', methods: ['POST'])]
public function create(
#[MapRequestToForm]
Post $post,
EntityManagerInterface $entityManager,
): JsonResponse {
$entityManager->persist($post);
$entityManager->flush();
return $this->json($post);
}
use App\Entity\Post;
use AzYouness\RequestToFormBundle\Attribute\MapRequestToForm;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/posts', methods: ['POST'])]
public function create(
#[MapRequestToForm]
Post $post,
): JsonResponse {
// $post is the submitted and validated form data.
return $this->json($post);
}
use App\Form\PostType;
public function create(
#[MapRequestToForm(PostType::class)]
Post $post,
): JsonResponse {
// ...
}
public function create(
#[MapRequestToForm]
?Post $post,
): JsonResponse {
// ...
}
use App\Entity\Post;
use AzYouness\RequestToFormBundle\Attribute\MapRequestToForm;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/posts/{id<\d+>}', methods: ['PUT'])]
public function update(
#[MapRequestToForm]
Post $post,
): JsonResponse {
// $post is first resolved from the {id} route parameter by Symfony's EntityValueResolver,
// then submitted to the form with the current request data.
return $this->json($post);
}
use App\Entity\Post;
use AzYouness\RequestToFormBundle\Attribute\MapRequestToForm;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/posts/{slug}', methods: ['PUT'])]
public function update(
#[MapRequestToForm]
#[MapEntity(mapping: ['slug' => 'slug'])]
Post $post,
): JsonResponse {
// $post is resolved by MapEntity, then submitted through the form.
return $this->json($post);
}
use App\Entity\Comment;
use App\Entity\Post;
use AzYouness\RequestToFormBundle\Attribute\MapRequestToForm;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/posts/{id<\d+>}/comments', methods: ['POST'])]
public function createComment(
Post $post,
#[MapEntity(disabled: true)]
#[MapRequestToForm]
Comment $comment,
): JsonResponse {
// $post is resolved from the {id} route parameter.
// $comment is created from the request body through the form.
return $this->json($comment);
}
public function update(
#[MapRequestToForm(clearMissing: false)]
Post $post,
): JsonResponse {
// ...
}
use App\Form\PostType;
use AzYouness\RequestToFormBundle\Attribute\MapRequestToForm;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
public function create(
#[MapRequestToForm(PostType::class)]
FormInterface $form,
): JsonResponse {
$post = $form->getData();
return $this->json($post);
}
use App\Entity\Post;
use App\Form\PostType;
use AzYouness\RequestToFormBundle\Attribute\MapRequestToForm;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
public function update(
Post $post,
#[MapRequestToForm(formType: PostType::class, dataArgument: 'post')]
FormInterface $form,
): JsonResponse {
// The form is submitted with $post as its initial data,
// so $post is updated with the current request data.
return $this->json($form->getData());
}
use AzYouness\RequestToFormBundle\Attribute\MapRequestToForm;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\JsonResponse;
public function rename(
#[MapRequestToForm(TextType::class)]
string $title,
): JsonResponse {
// $title is the submitted string.
return $this->json(['title' => $title]);
}
use App\Form\PostQueryType;
use AzYouness\RequestToFormBundle\Attribute\MapRequestToForm;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/posts', methods: ['GET'])]
public function index(
#[MapRequestToForm(PostQueryType::class)]
array $query,
): JsonResponse {
return $this->json($query);
}
use App\Entity\Post;
use AzYouness\RequestToFormBundle\RequestToFormMapper;
use Symfony\Component\HttpFoundation\JsonResponse;
public function create(RequestToFormMapper $mapper): JsonResponse
{
$post = new Post();
$post->setAuthor($this->getUser());
$mapper->handleCurrentRequest($post);
// $post is now the submitted and validated form data.
return $this->json($post);
}
use App\Form\PostType;
use AzYouness\RequestToFormBundle\RequestToFormMapper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
public function create(Request $request, RequestToFormMapper $mapper): JsonResponse
{
$form = $mapper->handle(
request: $request,
formType: PostType::class,
);
return $this->json($form->getData());
}