Core Components
The HTTP layer is organized into several key modules, each handling a specific part of the lifecycle:1. Controllers
Controllers are the entry points for your business logic. Each controller typically handles a single route and method.- Base Class:
AbstractApivalkController - Responsibility: Receive a typed Request and return a typed Response.
- Learn more: Controllers Documentation
2. Requests
Requests in Apivalk are more than just wrappers around$_GET and $_POST. They are strongly typed objects that automatically populate and validate themselves based on your documentation.
- Base Class:
AbstractApivalkRequest - Data Storage: Data is organized into “Bags” (Query, Body, Path, Header, File).
- Learn more: Requests Documentation
3. Responses
Apivalk encourages the use of semantic response objects rather than raw arrays or strings. This ensures consistency across your API.- Base Class:
AbstractApivalkResponse - Pre-defined Types:
BadRequestApivalkResponse,NotFoundApivalkResponse,UnauthorizedApivalkResponse, etc. - Learn more: Responses Documentation
4. HTTP Methods
HTTP verbs are represented as objects implementingMethodInterface. This allows the framework to handle routing and method validation cleanly.
- Implementations:
GetMethod,PostMethod,PutMethod,DeleteMethod,PatchMethod. - Factory:
MethodFactoryis used to instantiate these from strings.
5. Renderers
Renderers are responsible for taking anAbstractApivalkResponse and sending the appropriate headers and body to the client.
- Interface:
RendererInterface - Default:
JsonRenderer, which automatically handles JSON encoding andContent-Type: application/jsonheaders.
The HTTP Workflow
- Routing: The
Routermatches the incoming request URI and method to a specific Controller. - Request Preparation: The framework instantiates the controller and its associated
AbstractApivalkRequest. - Population & Validation: The Request object is populated from superglobals.
RequestValidationMiddlewarethen ensures the data matches the defined schema. - Execution: The Controller’s
__invoke()method is called with the populated Request. - Return: The Controller returns an
AbstractApivalkResponse. - Rendering: The
Renderer(e.g.,JsonRenderer) converts the Response object into the final HTTP output.
Specialized Helpers
Pagination
For APIs returning lists of data, Apivalk provides dedicated tools to handle offset-based pagination consistently.- Request side:
Paginatorhelper. - Response side:
ResponsePaginationobject. - Learn more: Pagination Documentation
Why use this structure?
- Predictability: Every endpoint follows the same pattern, making the codebase easier to navigate.
- Type Safety: You never have to wonder if a variable is a string or an integer; the framework guarantees it before your code runs.
- Zero Boilerplate: Automatic population and validation mean you focus only on business logic.
- Documentation Synergy: Because the HTTP layer is driven by the same definitions used for OpenAPI generation, your implementation and documentation are always in sync.