1. Go to this page and download the library: Download zvonchuk/phalcon-openapi 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/ */
zvonchuk / phalcon-openapi example snippets
use PhalconOpenApi\OpenApiModule;
$module = new OpenApiModule([
'title' => 'My API',
'version' => '1.0.0',
]);
$module->registerServices($di);
class UserController extends ApiController
{
// GET /users → 200 with array of User, operationId: listUsers
public function listAction(int $page = 1, int $limit = 20) { }
// POST /users → 201 Created + 422 Validation Error, operationId: createUser
public function createAction(CreateUserRequest $body) { }
// GET /users/{id} → 200 + 404 Not Found, operationId: getUser
public function getAction(int $id) { }
// DELETE /users/{id} → 204 No Content + 404, operationId: deleteUser
public function deleteAction(int $id) { }
}
use PhalconOpenApi\Attribute\{ApiTag, ApiIgnore, ApiResponse, ApiDescription, ApiSecurity, ApiPaginated};
#[ApiTag('Users')] // Group endpoints (class or method level)
#[ApiSecurity('bearerAuth')] // Require auth (class or method level)
class UserController extends ApiController
{
#[ApiDescription(
summary: 'List all users',
description: 'Returns a paginated list of users with optional filtering'
)]
#[ApiPaginated] // Wraps response in {data, total, page, per_page}
public function listAction(int $page = 1) { }
#[ApiIgnore] // Hide from spec
public function internalAction() { }
#[ApiResponse(409, ConflictResponse::class)] // Extra response code
public function createAction(CreateUserRequest $body) { }
}
use PhalconOpenApi\Attribute\{Email, Min, Max, StringLength, Format, Pattern, Enum, Url, NotBlank};
class CreateUserRequest
{
#[NotBlank]
#[StringLength(min: 1, max: 255)]
public string $name;
#[Email]
public string $email;
public ?string $phone = null; // nullable → type: ["string", "null"]
#[Min(1), Max(150)]
public int $age;
#[Enum(['active', 'inactive'])]
public string $status = 'active'; // → enum in OpenAPI schema
#[Url]
public ?string $website = null; // → format: uri in schema
#[Format('date')]
public ?string $birthDate = null; // → format: date in schema
#[Pattern('/^\+\d{10,15}$/')] // PCRE delimiters stripped for OpenAPI
public ?string $mobile = null;
}
class CreateOrderRequest
{
#[StringLength(min: 1)]
public string $orderNumber;
public AddressDto $shippingAddress; // → $ref + recursive validation
/** @var OrderItemDto[] */
public array $items = []; // → array with $ref + per-item validation
}
class AddressDto
{
#[NotBlank]
public string $street;
#[NotBlank]
public string $city;
#[Pattern('/^\d{5}$/')]
public string $zip;
}
use PhalconOpenApi\ApiController;
class UserController extends ApiController
{
public function getAction(int $id)
{
$user = User::findFirst($id);
if (!$user) {
return $this->notFound('User not found');
}
return $this->json($user);
}
public function createAction(CreateUserRequest $body)
{
// $body is already validated and hydrated automatically
$user = new User();
$user->assign((array) $body);
$user->save();
return $this->json($user, 201);
}
}