$npx -y skills add aligundogdu/symfony-hexagonal-skill --skill symfony-api-responseSymfony API response standardization — JSON payload format, exception handling, controllers, REST endpoints, error responses, debug mode. Triggers on: API, endpoint, controller, response, error handling, JSON, REST, API response, exception subscriber, HTTP
| 1 | # Symfony API Response Standard |
| 2 | |
| 3 | You are an expert in building standardized REST APIs within Symfony hexagonal architecture. |
| 4 | |
| 5 | ## When to Activate |
| 6 | |
| 7 | - User creates API endpoints or controllers |
| 8 | - User asks about response format or error handling |
| 9 | - User needs exception handling for APIs |
| 10 | - User mentions REST, JSON, or API design |
| 11 | |
| 12 | ## Standard JSON Payload |
| 13 | |
| 14 | ALL API responses use this format: |
| 15 | |
| 16 | ```json |
| 17 | { |
| 18 | "result": null, |
| 19 | "error": null, |
| 20 | "extra": null, |
| 21 | "status": 200 |
| 22 | } |
| 23 | ``` |
| 24 | |
| 25 | | Field | Type | Description | |
| 26 | |-------|------|-------------| |
| 27 | | `result` | `mixed` | Success data (null on error) | |
| 28 | | `error` | `?object` | Error details (null on success) | |
| 29 | | `extra` | `?object` | Metadata: pagination, debug info | |
| 30 | | `status` | `int` | HTTP status code | |
| 31 | |
| 32 | ### Success Response |
| 33 | ```json |
| 34 | { |
| 35 | "result": {"id": "uuid-123", "name": "John"}, |
| 36 | "error": null, |
| 37 | "extra": null, |
| 38 | "status": 200 |
| 39 | } |
| 40 | ``` |
| 41 | |
| 42 | ### Error Response |
| 43 | ```json |
| 44 | { |
| 45 | "result": null, |
| 46 | "error": {"code": "USER_NOT_FOUND", "message": "User not found"}, |
| 47 | "extra": null, |
| 48 | "status": 404 |
| 49 | } |
| 50 | ``` |
| 51 | |
| 52 | ### Debug Mode (APP_DEBUG=true) |
| 53 | ```json |
| 54 | { |
| 55 | "result": null, |
| 56 | "error": {"code": "INTERNAL_ERROR", "message": "Something went wrong"}, |
| 57 | "extra": {"debug": {"exception": "...", "trace": "..."}}, |
| 58 | "status": 500 |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ## Controller Pattern |
| 63 | |
| 64 | Controllers are thin — dispatch to buses only: |
| 65 | |
| 66 | ```php |
| 67 | namespace App\Presentation\{Module}\API; |
| 68 | |
| 69 | use App\Presentation\Shared\ApiResponseTrait; |
| 70 | use Symfony\Component\Routing\Attribute\Route; |
| 71 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 72 | |
| 73 | #[Route('/api/{module}')] |
| 74 | final class {Entity}Controller |
| 75 | { |
| 76 | use ApiResponseTrait; |
| 77 | |
| 78 | public function __construct( |
| 79 | private readonly MessageBusInterface $commandBus, |
| 80 | private readonly MessageBusInterface $queryBus, |
| 81 | ) { |
| 82 | } |
| 83 | |
| 84 | #[Route('', methods: ['POST'])] |
| 85 | #[IsGranted('ROLE_...')] // ALWAYS include |
| 86 | public function create(Request $request): JsonResponse |
| 87 | { |
| 88 | // 1. Parse input |
| 89 | // 2. Create command |
| 90 | // 3. Dispatch to command bus |
| 91 | // 4. Return success response |
| 92 | } |
| 93 | } |
| 94 | ``` |
| 95 | |
| 96 | ## References |
| 97 | |
| 98 | See `references/` for detailed guides: |
| 99 | - `payload-schema.md` — ApiResponseTrait, response helpers |
| 100 | - `exception-handling.md` — ExceptionSubscriber, error mapping |