1. Go to this page and download the library: Download psx/api-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/ */
declare(strict_types=1);
namespace App\Controller;
use App\Model\PostCollection;
use App\Model\Post;
use App\Model\Message;
use PSX\Api\Attribute\Body;
use PSX\Api\Attribute\Param;
use PSX\Api\Attribute\Query;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Attribute\Route;
final class PostController extends AbstractController
{
public function __construct(private PostService $service, private PostRepository $repository)
{
}
#[Route('/post', methods: ['GET'])]
public function getAll(#[Query] ?string $filter): PostCollection
{
return $this->repository->findAll($filter);
}
#[Route('/post/{id}', methods: ['GET'])]
public function get(#[Param] int $id): Post
{
return $this->repository->find($id);
}
#[Route('/post', methods: ['POST'])]
public function create(#[Body] Post $payload): Message
{
return $this->service->create($payload);
}
#[Route('/post/{id}', methods: ['PUT'])]
public function update(#[Param] int $id, #[Body] Post $payload): Message
{
return $this->service->update($id, $payload);
}
#[Route('/post/{id}', methods: ['DELETE'])]
public function delete(#[Param] int $id): Message
{
return $this->service->delete($id);
}
}
#[Route('/post', methods: ['POST'])]
public function create(#[Body] Json $body): Json
{
return $body;
}
#[Route('/post', methods: ['POST'])]
#[Outgoing(201, Message::class)]
#[Outgoing(400, Error::class)]
public function create(#[Body] Post $body): JsonResponse
{
if (empty($body->getTitle())) {
return new JsonResponse(new Error('An error occurred'), 400);
}
return new JsonResponse(new Message('Post successfully created')], 201);
}
php bin/console generate:sdk
php bin/console generate:model
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.