承接 yohan/rest-flex-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

yohan/rest-flex-php

最新稳定版本:1.0.8

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

Total Downloads Latest Stable Version

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2023-11-13