nextphp/rest
Composer 安装命令:
composer require nextphp/rest
包简介
The NextPHP Rest package provides powerful routing capabilities and HTTP handling for PHP developers. This package supports all RESTful methods (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, TRACE, CONNECT, PRI) and various response formats such as JSON, XML, HTML, TEXT, and CSV. It is part of the N
README 文档
README
The NextPHP Rest package provides powerful routing capabilities and HTTP handling for PHP developers. This package supports all RESTful methods (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, TRACE, CONNECT, PRI) and various response formats such as JSON, XML, HTML, TEXT, and CSV. It simplifies the creation of APIs by allowing developers to define routes and controllers using attributes, ensuring a clean and efficient codebase.
This package is part of the NextPHP Framework, a modern and lightweight PHP framework designed for performance and scalability. NextPHP aims to provide a comprehensive suite of tools and libraries to streamline the development process.
Features
- Support for all RESTful methods (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, TRACE, CONNECT, PRI)
- Response formats: JSON, XML, HTML, TEXT, CSV
- Attribute-based route definitions
- Middleware support
- Easy integration with existing projects
Installation
Installing via Composer
To install the NextPHP Rest package, you need to add it to your project using Composer.
composer require nextphp/rest
Example Project using NextPHP Rest
This is an example project demonstrating the usage of the NextPHP Rest package, which includes routing and HTTP handling capabilities.
Basic Usage Defining Routes Define routes using attributes to map HTTP methods to controller actions.
Usage
Using Controller
<?php namespace Example\Controller; use NextPHP\Rest\Http\Get; use NextPHP\Rest\Http\Post; use NextPHP\Rest\Http\Put; use NextPHP\Rest\Http\Delete; use NextPHP\Rest\Http\Patch; use NextPHP\Rest\Http\RouteGroup; use NextPHP\Rest\Http\Middleware; #[RouteGroup('/api/users')] class UserController { #[Get('/')] public function getAllUsers() { // logic to get all users } #[Post('/')] public function createUser() { // logic to create a user } #[Put('/{id}')] public function updateUser($id) { // logic to update a user } #[Delete('/{id}')] public function deleteUser($id) { // logic to delete a user } #[Patch('/{id}')] public function partiallyUpdateUser($id) { // logic to partially update a user } }
Advanced Entity Usage
Middleware Usage
Define middleware using attributes to apply them to routes. You can apply middleware to an entire controller class with #[Middleware(AuthMiddleware::class)], or to individual routes with method-specific attributes.
#[RouteGroup('/api')] #[Middleware(AuthMiddleware::class)] class UserController { #[Get('/users')] #[Middleware(AuthMiddleware::class)] public function getAllUsers() { // logic to get all users } }
Generate AuthMiddleware Class
AuthMiddleware handle JWT authentication check for HTTP request and response.
<?php namespace Example; use NextPHP\Rest\Http\Request; use NextPHP\Rest\Http\Response; use Firebase\JWT\JWT; use Firebase\JWT\Key; /** * Class AuthMiddleware * * A simple implementation of a PSR-7 http message interface and PSR-15 http handlers. * * Middleware for handling JWT authentication. * * @package NextPHP\Rest\Middleware */ class AuthMiddleware { /** * Handles the incoming request and checks for JWT authentication. * * @param Request $request The HTTP request. * @param Response $response The HTTP response. * @param callable $next The next middleware or controller. * @return Response The modified response. */ public function handle(Request $request, Response $response, callable $next): Response { $authHeader = $request->getHeaders()['Authorization'] ?? ''; if (!$authHeader) { return $response->withStatus(401)->withJSON(['error' => 'Unauthorized']); } list($jwt) = sscanf($authHeader, 'Bearer %s'); if (!$jwt) { return $response->withStatus(401)->withJSON(['error' => 'Unauthorized']); } try { $decoded = JWT::decode($jwt, new Key('your-secret-key', 'HS256')); // Token is valid, proceed with the request return $next($request, $response); } catch (\Exception $e) { return $response->withStatus(401)->withJSON(['error' => 'Unauthorized']); } } }
Service Layer Example
Services provide business logic and interact with repositories.
<?php namespace Example; #[Service(description: 'User management service')] class UserService { private UserRepository $userRepository; public function __construct(UserRepository $userRepository) { $this->userRepository = $userRepository; } #[Transactional] public function registerUser(array $userData): User { $user = new User(); $user->name = $userData['name']; $user->email = $userData['email']; $user->password = password_hash($userData['password'], PASSWORD_DEFAULT); $userArray = [ 'name' => $user->name, 'email' => $user->email, 'password' => $user->password, ]; $this->userRepository->save($userArray); return $user; } public function getAllUsers(): array { return $this->userRepository->findAll(); } public function getUserById(int $id): ?User { $userArray = $this->userRepository->find($id); if (!$userArray) { return null; } $user = new User(); $user->id = $userArray['id']; $user->name = $userArray['name']; $user->email = $userArray['email']; $user->password = $userArray['password'] ?? ''; return $user; } public function updateUser(int $id, array $data): ?User { $user = $this->getUserById($id); if (!$user) { return null; } foreach ($data as $key => $value) { if (property_exists($user, $key)) { $user->$key = $value; } } $userArray = get_object_vars($user); $this->userRepository->update($id, $userArray); return $user; } public function deleteUser(int $id): bool { $user = $this->getUserById($id); if (!$user) { return false; } $this->userRepository->delete($id); return true; } }
Example Project
Example for your Project Structure
example/
├── src/
│ ├── Entity/User.php
│ ├── Repository/UserRepository.php
│ ├── Service/UserService.php
│ ├── Resource/UserResource.php
├── example.php
├── composer.json
└── README.md
Example or example.php
To use the NextPHP Rest package, you can create an index.php file and use the router to handle various HTTP requests. Here is an example of how you can do this:
Example index.php
<?php require_once __DIR__ . '/vendor/autoload.php'; use NextPHP\Rest\DI\Container; use NextPHP\Rest\Router; use NextPHP\Rest\Http\Request; use NextPHP\Rest\Http\Response; use NextPHP\App\Resource\UserResource; use NextPHP\App\Resource\PostResource; $container = new Container(); $router = new Router([ 'baseUri' => '/nextphp-beta', 'allowedOrigins' => [ 'http://allowed-origin.com' => ['GET', 'POST'], 'http://another-allowed-origin.com' => ['GET', 'PUT'], '*' => ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'TRACE', 'CONNECT', 'PRI'] ] ], $container); // DI $router->registerRoutesFromController(UserResource::class); $router->registerRoutesFromController(PostResource::class); $uri = $_SERVER['REQUEST_URI']; $method = $_SERVER['REQUEST_METHOD']; $request = new Request($method, $uri, getallheaders(), file_get_contents('php://input'), $_GET, $_POST); $response = new Response(); $response = $router->dispatch($request, $response); if ($response) { http_response_code($response->getStatusCode()); foreach ($response->getHeaders() as $name => $value) { header("$name: $value"); } echo $response->getBody(); } else { http_response_code(500); echo json_encode(['error' => 'Internal Server Error', 'message' => 'No response returned.']); }
Contributing
We welcome contributions! Here’s how you can help:
- Report Issues: Found a bug? Report it on GitHub.
- Suggest Features: Have an idea? Share it with us.
- Submit Pull Requests: Improve the codebase.
- Enhance Documentation: Help us improve our docs.
For more details, see our Contribution Guidelines.
Resources
Join Our Community
Contact Us
- Email: support@nextphp.io
- Forum: NextPHP Mastodon
- GitHub Issues: NextPHP GitHub
Thank you for being part of the NextPHP community!
FAQ
Q: How do I define a route?
A: Use the #[Get], #[Post], #[Put], #[Delete], #[Patch], etc. attributes to define a method as a route handler. Use #[RouteGroup] to define a common prefix for a group of routes.
For more details, see our FAQ.
nextphp/rest 适用场景与选型建议
nextphp/rest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「routing」 「php」 「json」 「xml」 「text」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nextphp/rest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nextphp/rest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nextphp/rest 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Write down your routing mapping at one place
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
Alfabank REST API integration
A router designed for web requests for the Monolith framework
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-04