Skip to main content

Creating a Controller

Every controller must extend AbstractApivalkController and implement four key methods:
  1. getRoute(): Defines the path and HTTP method.
  2. getRequestClass(): Specifies the AbstractApivalkRequest class used for this endpoint.
  3. getResponseClasses(): Lists the possible AbstractApivalkResponse classes this controller can return.
  4. __invoke(): The main execution logic.

Example

Dependency Injection

If you configured a PSR-11 container, you can use constructor injection:

Route Discovery

Apivalk uses ClassLocator to scan your controller directory. You don’t need to register routes manually; the framework discovers them automatically by calling getRoute() on any class extending AbstractApivalkController.

Pagination

For endpoints returning lists of data, see the Pagination guide.

Resource Controllers (CRUD)

If your endpoint follows a standard RESTful CRUD pattern against a single entity, you don’t need to author a controller class like the one above by hand. Apivalk ships five abstract base controllers — AbstractCreateResourceController, AbstractViewResourceController, AbstractUpdateResourceController, AbstractDeleteResourceController, AbstractListResourceController — that derive their route, request class, response classes, and OpenAPI documentation from an AbstractResource declaration. A minimal list controller looks like this:
See the full Resources section for how to declare a resource and wire up all five CRUD endpoints.