forgr/v1

Minimal API-as-a-Service platform - Write functions, register them, use them as APIs

Maintainers

Package info

github.com/chukwunonsoprosper/forgr

Type:project

pkg:composer/forgr/v1

Statistics

Installs: 11

Dependents: 0

Suggesters: 0

Stars: 31

Open Issues: 0

v1.0.0 2025-08-09 14:22 UTC

This package is auto-updated.

Last update: 2026-03-15 17:08:34 UTC


README

Turn PHP functions into REST APIs with one line of code.

Write a function, register it, call it as an API.

Quick Example

<?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"}'

Quick Start

composer create-project forgr/v1 my-api
cd my-api
php -S localhost:8080

Test the included example:

curl -X POST localhost:8080 -H "X-Route: user"

How It Works

  1. Create a .php file in the routes/ folder
  2. Write a function that takes Request and returns Response
  3. Register it: get('function_name') or post('function_name')
  4. Call via HTTP with X-Route: function_name header

Registration Functions

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 & Response API

Request Methods

$request->getBody()        // JSON body as array
$request->getQuery('key')       // URL parameter
$request->getMethod()      // HTTP method
$request->getBearerToken() // Authorization header

Response Methods

Response::success($data)       // 200 with data
Response::created($data)       // 201 Created
Response::error($msg, $code)   // Error response
Response::notFound()           // 404 Not Found

Response Format

{
  "success": true,
  "message": "Success",
  "data": { "your": "data" },
  "timestamp": "2025-08-09 14:00:00"
}

Features

  • 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

Requirements

  • PHP 8.1+
  • Composer

License

MIT