Turn PHP functions into REST APIs with one line of code.
Write a function, register it, call it as an API.
<?php
// routes/hello.php
use Forgr\Core\Request;
use Forgr\Core\Response;
function hello(Request $request): Response {
$data = $request->getBody();
$name = $data['name'] ?? 'World';
return Response::success(['message' => "Hello {$name}!"]);
}
// Register as API endpoint
post('hello');Call it:
curl -X POST localhost:8080 \
-H "X-Route: hello" \
-d '{"name": "John"}'composer create-project forgr/v1 my-api
cd my-api
php -S localhost:8080Test the included example:
curl -X POST localhost:8080 -H "X-Route: user"- Create a
.phpfile in theroutes/folder - Write a function that takes
Requestand returnsResponse - Register it:
get('function_name')orpost('function_name') - Call via HTTP with
X-Route: function_nameheader
get('function_name'); // GET route
post('function_name'); // POST route
put('function_name'); // PUT route
delete('function_name'); // DELETE route
route('name', 'PATCH'); // Custom HTTP method$request->getBody() // JSON body as array
$request->getQuery('key') // URL parameter
$request->getMethod() // HTTP method
$request->getBearerToken() // Authorization headerResponse::success($data) // 200 with data
Response::created($data) // 201 Created
Response::error($msg, $code) // Error response
Response::notFound() // 404 Not Found{
"success": true,
"message": "Success",
"data": { "your": "data" },
"timestamp": "2025-08-09 14:00:00"
}- ✅ Zero Configuration - Works immediately
- ✅ Function-based - No classes or frameworks
- ✅ CORS Ready - Frontend integration built-in
- ✅ HTTP Client - Make external API calls with Guzzle
- ✅ Consistent JSON - Structured response format
- ✅ Error Handling - Automatic error responses
- PHP 8.1+
- Composer
MIT