# PHP

A Slim 4 REST API starter in PHP 8.2 for PHP developers who want a small JSON API with PSR-7 requests and responses. It includes single-action route classes, a health check, PHPUnit tests and a Dockerfile on the official PHP Apache image.

- Framework: PHP (PHP)
- Category: Starters / REST APIs
- Creates: a server app
- Plan: Free
- Price: Free
- Author: Light Cloud
- Page: https://templates.light-cloud.com/starters/rest-apis/slim-php-api-starter
- Updated: 2026-11-21

## Features

- Slim 4.15 with slim/psr7; GET / returns API info and GET /health returns status, timestamp and uptime
- One invokable class per route in src/Action/, registered in config/routes.php
- config/bootstrap.php builds the app once for both public/index.php and the tests
- Slim's error middleware logs errors and answers in the format the Accept header asks for: JSON, XML, HTML or text
- APP_DEBUG=true shows error details in responses; it is off by default
- PHPUnit 11 tests hand requests to the real app without a web server, including a 404 check
- Dockerfile on php:8.2-apache with mod_rewrite, the document root on public/ and Apache's port set from PORT at start
- public/.htaccess sends every request that is not a real file to the front controller

## What you need

- Free plan: it runs as a server app, and the Free plan includes 3 server apps
- To run it locally: PHP 8.2 or newer and Composer
- No API keys: it starts with nothing to configure
- Optional: APP_DEBUG=true to see error details while you work; keep it false in production

## About

Requests go from Apache (or PHP's built-in server via composer start) to public/index.php, which loads the app from config/bootstrap.php; the routing middleware then hands the PSR-7 request to the matching action class. Because PHP starts fresh for every request, the uptime in /health stays close to zero, so most people adapt or drop that field first, then add their own actions and, for POST bodies, addBodyParsingMiddleware(). The repo includes a .mcp.json for the Light Cloud MCP server, and Light Cloud builds the Dockerfile and redeploys on each push to your copy.

## Questions

### How do I add a route in Slim 4?

Add an invokable class in src/Action/ that takes the request and response and returns the response, like HomeAction.php. Register it in config/routes.php with $app->get('/path', YourAction::class).

### How do I read a JSON request body in Slim?

Add $app->addBodyParsingMiddleware(); in config/bootstrap.php. $request->getParsedBody() then returns the decoded JSON as an array.

### Why do Slim errors come back as HTML instead of JSON?

Slim's error middleware picks the format from the Accept header. Send Accept: application/json and errors, including the 404 for unknown routes, come back as JSON.

### Should I use Slim or Laravel for a small PHP API?

Slim gives you routing, middleware and PSR-7 and nothing else, which suits small APIs and services. Laravel brings an ORM, queues and much more, which pays off on larger apps.

### Can I host this Slim app on ordinary PHP hosting?

Yes. Run composer install --no-dev and point the document root at public/: Apache uses the included .htaccess, and on nginx use try_files $uri /index.php?$query_string. The Docker image runs on any container host too.
