定制 forgr/v1 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

forgr/v1

最新稳定版本:v1.0.0

Composer 安装命令:

composer create-project forgr/v1

包简介

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

README 文档

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

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 30
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 30
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-09