benjaco / highway
简单快速的PHP路由器
1.2.1
2017-07-12 09:19 UTC
Requires
- php: >=5.5.0
This package is not auto-updated.
Last update: 2024-09-29 04:14:28 UTC
README
这个PHP路由器的速度比slim3快25倍,但highway没有像中间件这样的高级功能,它是一个纯粹的PHP路由器
它很灵活,例如,可以在组的开始处添加代码
速度的部分原因在于,此路由器在运行之前不会收集所有路由,而是在路由定义后立即运行,如果URL不匹配特定组,则该组的回调函数将不会执行
安装
使用Composer安装。
{ "require": { "benjaco/highway": "1.*" } }
用法
include "vendor/autoload.php"; use \benjaco\Highway\Highway; // setup can be called if you dont want the default parameter, or Highway::$url can be set // Highway::set_up(false); Highway::get("/", function () { echo "index"; }); Highway::get("/testvar/{}", function ($var) { echo $var; }); Highway::group("/user", function () { Highway::get("/{name}", function () { echo "Wellcome " . $_GET['name']; }); }); // its posible to define a regex the parameter must match, but the regex cant contain capture groups Highway::addOption("danish_phone_number", "(?:\+45)?\d{8}"); Highway::get("/number/{phone:danish_phone_number}", function (){ echo "Phone nr: " . $_GET['phone']; }); // int is a standard one there can be used as well Highway::get("/testint/{:int}", function ($var) { echo $var; }); // optional parameter with default value function using_function($id = "No parameter"){ echo "Route using function ". $id; } Highway::get("/using_function", "using_function"); Highway::get("/using_function/{}", "using_function"); // using static functions in a class // it will autoload the class only if its needed, if you are using a autoloader Highway::get("/using_class", "UsingClass::Route"); Highway::get("/using_class/{}", "UsingClass::Route"); // using methods from a instance of a class $classForRoutes = new UsingClass(); Highway::get("/using_class_init", [$classForRoutes, "AnotherRoute"] ); Highway::get("/using_class_init/{}", [$classForRoutes, "AnotherRoute"] ); Highway::not_found(function () { echo 404; });
合作
所有问题和拉取请求都欢迎