Slim is a micro-framework for HTTP applications. It provides routing and middleware while leaving many architectural choices to the application. That flexibility is useful when the API has a narrow scope, but it also means the team must define its own conventions.
Assemble the application
Create the Slim application with a compatible PSR-7 implementation and configure error middleware. Register routes in a dedicated place and keep route definitions readable. Route handlers receive request and response objects and should return a response.
Use middleware for cross-cutting concerns
Authentication, request identifiers, CORS policy and structured logging can be implemented as middleware when they apply across routes. Middleware order matters: document which components run before routing and which wrap route execution.
Keep business logic outside handlers
Handlers should parse validated input, call an application service and serialize the result. Use dependency injection to supply services and repositories. Avoid embedding SQL and complex branching in anonymous route callbacks.
Test the HTTP contract
Test successful responses, malformed requests, missing credentials and unexpected exceptions. Assert status codes, content types and stable response fields.
Further reading
- Slim documentation: https://www.slimframework.com/docs/v4/