yohan/rest-flex-php
Composer 安装命令:
composer require yohan/rest-flex-php
包简介
RestFlexPHP is a robust PHP package meticulously crafted to simplify the intricacies of handling HTTP requests, with a primary focus on supercharging your experience in managing RESTful APIs. This package provides an elegant and developer-friendly interface, allowing you to effortlessly send request
README 文档
README
RestFlexPHP is a lightweight PHP package designed to simplify handling RESTful requests and responses. It includes a flexible controller class (RestFlexController) for routing requests and a request class (RestFlexRequest) for encapsulating request-related data.
Installation
You can install RestFlexPHP using Composer:
composer require yohan/rest-flex-php
Setting up URL Rewriting
To enable URL rewriting for your RESTful API, you need to create an .htaccess file in the same directory as your main logic file (e.g., index.php). The file should contain the following mod_rewrite rules:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /path-from-the-root/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ ./index.php?url=$1 [QSA,L] </IfModule>
Usage
RestFlexController
The RestFlexController class provides a simple routing mechanism for handling HTTP requests. You can define routes using the get, post, put, and delete methods.
Example:
<?php use RestFlexPHP\RestFlexController; use RestFlexPHP\RestFlexRequest; // Retrieving a controller instance $controller = RestFlexController::getController(); // Define a route for GET requests $controller->get('/users/{id}', function (RestFlexRequest $request) { // Handle the GET request for the "/users/{id}" path $userId = $request->pathVariables['id']; // Your custom logic here... // Finally send the response to the client $request->sendResponse($data, HttpStatus::OK); }); // Other route definitions... // Handle unmatched routes $controller->noMapping(function (RestFlexRequest $request) { // Handle unmatched routes or provide a default 404 response $request->sendResponse('Route not found', HttpStatus::NOT_FOUND); }); ?>
Example: Separating different routes with separate classes.
Here's a simple example demonstrating how to use RestFlexPHP to create a basic UserController with user-related routes.
UserController.php
<?php use RestFlexPHP\RestFlexController as RFC; use RestFlexPHP\RestFlexRequest as RFR; class UserController { public function __construct(RFC $controller) { $controller->get('users/getAllUsers', [$this, 'getAllUsers']); $controller->get('users/getUser/{id}', [$this, 'getUser']); } public function getAllUsers(RFR $req) { $req->sendResponse(/* all your users */); } public function getUser(RFR $req) { $userId = $req->pathVariables['id']; // $user = your logic to find user by user id from the database. $req->sendResponse($user); } } ?>
index.php
<?php use RestFlexPHP\RestFlexController; $controller = RestFlexController::getController(); // Instantiate UserController, which will define routes new UserController($controller); // Handle unmatched routes $controller->noMapping(); ?>
RestFlexRequest
The RestFlexRequest class encapsulates request-related data and provides a convenient method (sendResponse) for sending JSON responses.
Example:
$controller->put('/users/{id}', function (RestFlexRequest $request) { // Access path variables using pathVariables array $userId = $request->pathVariables['id']; // Access request body using body property $data = $request->body; // Access URL query parameters using params property $params = $request->params; // Finally send the response back to the client $request->sendResponse($data, HttpStatus::OK); });
Contributing
If you find a bug, have a feature request, or want to contribute, please open an issue or submit a pull request. Contributions are welcome!
License
This package is licensed under the Apache-2.0 License
yohan/rest-flex-php 适用场景与选型建议
yohan/rest-flex-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 yohan/rest-flex-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yohan/rest-flex-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2023-11-13