zkrati/router

PHP 路由器,简单到极致

1.0 2018-07-10 14:53 UTC

This package is not auto-updated.

Last update: 2024-09-25 16:55:13 UTC


README

PHP 路由器,简单到极致...

  • 简单快速,但功能复杂的路由
  • 支持静态和动态路由
  • 支持GET、POST、PUT、DELETE和PATCH请求

版本

1.0

基本用法

$router = new Zkrati\Routing\SimpleRouter();

$router->get("/test", function() {
    // handle GET request at /test
});

$router->post("/test", function() {
    // handle POST request at /test
});

$router->run();

简单吧?

您还可以使用其他处理程序声明

$router->get("/test/path", "Class:methodName");
// this code will create an instance of Class and call it's method methodName

$router->get("/test/path", "Namespace\Class:methodName");
// if you are using namespaces

如果您出于某种原因需要创建自己的实例,您可以传递已创建的实例

$instance = new Class();
$router->get("/testuju/path", array($instance, "methodName"));
// this code will use the given instance of Class and call it's method methodName

变量

您可能需要使用一些变量来映射路由。这从未如此简单。

$router->get("/test/<variable>/<next_variable>/path", function($variables) {
    // variables <variable> and <next_variable> are available in array $variables by it's keys
    // for example with url /test/example/showcase/path
    
    echo $variables["variable"];      // will output "example"
    echo $variables["next_variable"]; // will output "showcase"
});

参数

如果您需要将参数添加到URL中,您也可以。它将作为第二个参数传递给您的处理程序。

$router->get("/test/<variable>/<next_variable>/path", function($variables, $params) {
    // for url /test/example/showcase/path?optional=true&param=john will passed array $params look like:
    // array(2) {
    //     ["optional"] => string(4) "true"
    //     ["param"] => string(4) "john"
    //   }
});

URL通配符

如果您处于仅对路径前半部分感兴趣的情况,您可以使用通配符。

$router->get("/test/path/*", "Class:methodName");
// this code will match all paths starting /test/path/ for example:
//  - /test/path/first
//  - /test/path/second
//  - /test/path/every/other/path

您甚至可以将它用作所有请求的通配符,例如

$router->option("*", "Class:methodName");
// This will handle all option requests

您还可以将其与变量结合使用

$router->get("/test/<variable>/*", "Class:methodName");

头部信息

有时了解请求头部信息很有用。您再也不需要到处搜索了。只需将第三个参数添加到您的处理程序函数中即可。

$router->get("/test/<variable>/<next_variable>/path", function($variables, $params, $headers) {
    // variable $headers is array which contains all request headers 
});

异常

将路由声明包装在try catch中是一个好主意,因为在不合法的处理程序的情况下,它会抛出InvalidHandlerException。

try{

    $router->get("/test/", "Class:firstMethod");
    $router->get("/other/route", "Class:secondMethod");
    $router->get("/cool/route", "invalid handler");
    
} catch(InvalidHandlerException $e) {
    echo $e->getMessage();
}

此外,$router->run(); 当它找不到当前URL的路由时,会抛出RouteNotFoundException。

try{
    $router->run();
} catch(RouteNotFoundException $e) {
    echo $e->getMessage();
}

实例

如果您有一个自定义类,该类管理应用程序中所有其他类的实例,并且您想使用字符串处理程序定义,您可以将您的实例化器传递给路由器。路由器将从您的自定义实例化器获取类实例。

$router->setInstantiator($instantiator, "getInstance");
// where $instantiator is your custom instantiator and "getInstance" is name of it´s method to get instance

待办事项

  • 定义变量类型和正则表达式
  • 添加缓存支持

许可证

MIT