fastpress / router
Fastpress 框架的一个简单而强大的路由组件,能够在 PHP 应用程序中实现高效的 HTTP 路由。
0.1.0
2024-02-14 19:24 UTC
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-09-15 18:07:44 UTC
README
Fastpress Router 是 PHP 的一个简单而强大的路由类,是 Fastpress 框架的一部分。它允许您为应用程序定义路由并高效地处理 HTTP 请求。
特性
- 支持 GET、POST、PUT 和 DELETE HTTP 方法。
- 轻松定义路由模式。
- 支持 RESTful 路由。
- 可定制的路由模式。
要求
- PHP 7.0 或更高版本。
安装
请在此插入安装此路由类或 Fastpress 框架的说明。
使用方法
基本使用
require 'path/to/Router.php'; $router = new Fastpress\Routing\Router(); // Define a GET route $router->get('/home', function() { return 'Welcome to the homepage!'; }); // Handle the request // Assuming $server and $post are your $_SERVER and $_POST variables $result = $router->match($server, $post); if ($result) { // Route was matched list($args, $callable) = $result; call_user_func_array($callable, $args); } else { // No route was matched header("HTTP/1.0 404 Not Found"); }
定义路由
您可以为不同的 HTTP 方法定义路由
$router->get($uri, $callable); $router->post($uri, $callable); $router->put($uri, $callable); $router->delete($uri, $callable);
路由模式
您可以在路由模式中使用以下占位符
- :any - 匹配任何字符
- :id - 匹配数字字符
- :slug - 匹配字母数字字符、破折号和下划线
- :name - 匹配字母字符
- :url - 匹配字母数字字符和 URL 友好符号
贡献
欢迎贡献!请随时提交拉取请求或打开问题以改进此库。
许可
此库是开源软件,许可协议为 MIT 许可。
支持
如果您遇到任何问题或有问题,请在 GitHub 的问题部分提出。