joseluisq / ruta
一个轻量级的PHP HTTP路由库,无外部依赖
v1.0.0-beta.6
2022-11-16 09:03 UTC
Requires
- php: >=8.0
Requires (Dev)
- pestphp/pest: ^1.22
- pestphp/pest-dev-tools: 1.x
- pestphp/pest-plugin-parallel: ^1.0
- subiabre/appgati: ^2.0
This package is auto-updated.
Last update: 2024-09-27 00:50:52 UTC
README
一个轻量级的PHP HTTP路由库。(工作中)
特性
- HTTP状态码
- HTTP方法
- HTTP头部
- HTTP请求
- 处理
multipart/form-data
请求数据 - 处理
x-www-form-urlencoded
请求数据 - 处理
application/xml
请求数据 - 处理
application/json
请求数据 - 处理
query
URI请求参数 - 访问请求的
path
和headers
- 处理
- HTTP响应
- HTML、JSON、XML或文本响应
- HTTP重定向
- HTTP路由
- 带有占位符参数的路由:
/abc/{some}
- 处理多个有效的HTTP方法
- 处理任何有效的HTTP方法
- 处理不匹配的路由(404)
- 支持回调或类/方法样式
- 回调或类/方法处理器上的可选和顺序无关的参数
- 正则表达式支持:
/abc/regex(id=^[0-9]+$)
- 可选路由参数:
/abc/{some?}
- 回退路由
- 路由缓存
- 带有占位符参数的路由:
- 中间件
- 在路由前检查或过滤有效请求
- 在路由后检查或过滤有效请求
查看基于Ruta的微框架Leap。
要求
PHP 8.0或更高版本。
安装
通过Composer安装
composer require joseluisq/ruta:dev-master
用法
<?php declare(strict_types=1); require 'vendor/autoload.php'; use Ruta\Header; use Ruta\Method; use Ruta\Ruta; use Ruta\Request; use Ruta\Response; use Ruta\Status; // 1. Callback style // NOTE: `Request`, `Response`, `array` (slug arguments) are passed to the callback. // However they are optional and their order can be modified. See more examples below. Ruta::get('/home/hola', function (Request $req, Response $res) { $res->json([ 'host' => $req->header(Header::Host), 'headers' => $req->headers(), ]); }); Ruta::get('/home/hola/redirect', function (Response $res) { $res->redirect('/home/aaa/some/bbb'); }); Ruta::get('/reg/regex(id=^[0-9]+$)/exp', function (Response $res, array $args) { $res->json(['args' => $args]); }); Ruta::post('/home/{path3}/some2', function (Response $res) { $res->json(['post_data' => 11010101010]); }); Ruta::some('/home/some', [Method::POST, Method::PUT], function (Request $req, Response $res) { $res->json(['only' => $req->method()]); }); Ruta::any('/home/methods', function (Request $req, Response $res) { $res->json(['method' => $req->method()]); }); Ruta::post('/home/{path}', function (Response $res) { $res ->header('X-Header-One', 'Header Value 1') ->header('X-Header-Two', 'Header Value 2') ->json(['some_data' => 223424234]); }); // 2. class/method style class HomeCtrl { public function index(Request $req, Response $res, array $args) { // 2.1 $args contains route placeholder values if (array_key_exists('path1', $args)) { // do something... } // 2.2. Get data provided via `multipart/form-data` $data = $req->multipart(); // 2.3. Get all headers $data = $req->headers(); // 2.4. Get a single header $data = $req->header("Host"); // 2.5. Get data provided via `application/x-www-form-urlencoded` $data = $req->urlencoded(); // 2.6. Get data provided via `application/json` $data = $req->json(); // 2.7. Get data provided via `application/xml` $data = $req->xml(); // 2.8. Get query data $data = $req->query(); $res->json(['data' => 'Message from a class!']); } // Custom 404 reply public function not_found(Response $res) { $res ->status(Status::NotFound) ->text("404 - Page Not Found!"); } } Ruta::get('/home/{path1}/some/{path2}', [HomeCtrl::class, 'index']); // 3. Handle 404 not found routes Ruta::not_found([HomeCtrl::class, 'not_found']);
代码示例
文件:example/nginx/public/index.php
# Or run example using Docker + Nginx server
make compose-up
# Run example using the PHP built-in server
make container-dev
现在导航到,例如:https://:8088/home/hola
贡献
贡献
除非你明确说明,否则你提交给当前工作的任何有意贡献,根据Apache-2.0许可证的定义,应按以下方式双许可,而不附加任何额外条款或条件。
请随意发送一些Pull request或issue。
许可证
本作品主要在以下两个条款下分发:MIT许可证和Apache许可证(版本2.0)。
© 2021-present Jose Quintana