1. Go to this page and download the library: Download gdnacho/poob 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/ */
gdnacho / poob example snippets
use App\Api\InputDto\UsernameInput;
use App\Api\OutputDto\UsernameOutput;
use App\Repository\UserRepository;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/api/users', name: 'app_user_get')]
public function getUser(
UsernameInput $data,
UserRepository $userRepository
): JsonResponse {
// Find the user entity using the input DTO
$user = $userRepository->findOneBy(['username' => $data->username]);
// Return a response DTO, serialized to JSON automatically
return $this->json(
$user ? UsernameOutput::from($user) : ['error' => 'User not found']
);
}
public function getUser(
array $requestData,
UserRepository $userRepository
): JsonResponse {
}
// src/Api/InputDto/UsernameInput.php
class UsernameInput
{
#[Field\UsernameField]
public $username;
/**
* This method runs after attribute validation and allows
* implementing custom logic that depends on multiple fields, or mutate data as needed.
*/
public function extra(): void
{
}
}
// src/Api/Field/UsernameField.php
#[\Attribute]
class UsernameField extends Assert\Compound
{
protected function getConstraints(array $options): array
{
return [
new Assert\NotBlank(),
new Assert\Type('string'),
new Assert\Length(min: 2, max: 24),
new Assert\Regex(
pattern: '/^[A-Za-z0-9_]+$/',
message: 'Username may only contain letters, numbers, and underscores.'
),
];
}
}
// src/Api/OutputDto/UsernameOutput.php
class UsernameOutput extends OutputDto
{
public function __construct(
public $username,
) {
}
}
#[Route('/api/user/{id}', methods: ['GET'], name: 'app_user_get')]
#[Summary('Get user')] // Adds a summary
public function get(string $id, ListUserInput $data): JsonResponse
class CreateUserInput extends InputDto
{
#[Field\UsernameField]
#[Description('Username must be 3-24 characters long')] // Adds a description
public string $username;
}
final class UserController extends AbstractController
{
public function __construct(
private UserRepository $repo,
) {
}
#[Route('/api/user/{id}', methods: ['GET'], name: 'app_user_get')]
#[Summary('Get user')]
public function get(string $id, ListUserInput $data): JsonResponse
{
$user = $this->repo->find($id);
if (!$user) {
return $this->json(['error' => 'Not found'], 404);
}
return $this->json(ListUserOutput::from($user));
}
#[Route('/api/user', methods: ['GET'], name: 'app_user_list')]
#[Summary('List user')]
public function list(ListUserInput $data): JsonResponse
{
$users = $this->repo->findByFilters($data);
return $this->json(ListUserOutput::collection($users));
}
#[Route('/api/user', methods: ['POST'], name: 'app_user_create')]
#[Summary('Create user')]
public function create(CreateUserInput $data, EntityManagerInterface $em): JsonResponse
{
$user = new User();
$user->setUsername($data->username);
$user->setAge($data->age);
$user->setEmail($data->email ?? null);
$em->persist($user);
$em->flush();
return $this->json(CreateUserOutput::from($user), 201);
}
#[Route('/api/user/{id}', methods: ['PATCH'], name: 'app_user_update')]
#[Summary('Update user')]
public function update(string $id, UpdateUserInput $data, EntityManagerInterface $em): JsonResponse
{
$user = $this->repo->find($id);
if (!$user) {
return $this->json(['error' => 'Not found'], 404);
}
if ($data->username !== null) $user->setUsername($data->username);
if ($data->age !== null) $user->setAge($data->age);
if ($data->email !== null) $user->setEmail($data->email);
$em->flush();
return $this->json(CreateUserOutput::from($user));
}
#[Route('/api/user/{id}', methods: ['DELETE'], name: 'app_user_delete')]
#[Summary('Delete user')]
public function delete(string $id, EntityManagerInterface $em): JsonResponse
{
$user = $this->repo->find($id);
if (!$user) {
return $this->json(['error' => 'Not found'], 404);
}
$em->remove($user);
$em->flush();
return $this->json('', 204);
}
}
bash
php bin/console poob:init
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.