Documentation · Chapter 12

Receive and return predictable JSON.

A request object validates incoming data; a resource decides the public response shape; ApiResponse keeps errors consistent.

Every lesson starts from the problem, explains the mechanism, then gives a concrete verification.

Validate before the controller works

ApiRequest uses Validator rules and produces a validation exception translated to a 422 response with field errors.

phpaml — zsh
return ApiResponse::success([
    'data' => MovieResource::collection($movies),
]);

Return a resource, not an entity

A resource can rename fields, format dates, hide secrets, and add links. This becomes the stable public contract even if the entity changes.

Keep errors stable

Clients should be able to handle an error code and message consistently. Debug details appear only when explicitly enabled.