ride / lib-router
Ride框架的路由库
1.4.1
2018-02-15 16:04 UTC
This package is auto-updated.
Last update: 2024-09-12 23:28:22 UTC
README
Ride PHP框架的路由库。
路由用于将传入的HTTP请求转换为回调。
本库包含内容
路由
一个路由定义了一个带有回调的请求路径。
路由的定义提供了两种向动作传递参数的方式
- 路径中的占位符带有变量名称,该变量名称用于动态值。变量的名称应介于%之间。
- 定义中的静态值
你可以选择性地为路由设置一个ID,以便在代码中轻松检索。通过使用ID,你可以在不更改代码的情况下通过配置覆盖路径。
通过在回调中只实现一个动作来保持代码的整洁。将路由限制为特定的或多个请求方法(GET、POST等)可以帮助你实现这一点。
你可以为路由设置一个基本URL,以将动作限制在特定的域名。
也可以为路由设置一个区域设置。这在后续过程中对本地化路径进行操作时很有用。
你可以通过向路由添加权限来帮助你的安全层。路由器本身将不考虑这些权限。
别名
别名定义了具有查询参数的现有路径的别名路径。
可以强制执行以将原始路径重定向到别名。
RouteContainer
RouteContainer是路由的集合。它提供了一个简单的接口来管理路由和别名。
使用路由容器来生成URL,以便处理别名。
Router
Router是实际将传入请求转换为路由的组件。它是一个简单的接口,但库中添加了通用的实现。
RouterResult
在路由器上执行路由动作的结果是RouterResult对象。
此对象有三种可能的状态
- 空:没有路由匹配传入请求
- 设置了允许的方法:一个路由匹配了,但不是对传入请求方法的匹配
- 设置了别名:匹配了强制别名,请求应该被重定向
- 设置了路由:一个路由匹配了,应该调用回调
Url
Url是一个可变对象,用于更新和操作生成的URL。
代码示例
查看此代码示例以了解本库的可行性
<?php use ride\library\router\GenericRouter; use ride\library\router\RouteContainer; // create a route container $routeContainer = new RouteContainer(); // create a route with a path and a php callback $route = $routeContainer->createRoute('/path/to/%action%', 'callback', 'id'); // single method allowed $route->setAllowedMethods('GET'); // multiple methods allowed, case does not matter $route->setAllowedMethods(array('GET', 'post')); // add the route to the route container $routeContainer->setRoute($route); // create an alias $alias = $routeContainer->createAlias('/path/to/content', '/ptc'); // add the alias to the route container $routeContainer->setAlias($alias); // create the router $router = new GenericRouter($routeContainer); // set a default action for the / request $router->setDefaultCallback('callback'); // match a route $result = $router->route('GET', '/foo/bar'); // no match $result->isEmpty(); // true // let's try again $result = $router->route('PUT', '/path/to/content'); // a match but nothing to invoke $result->isEmpty(); // false $result->getAlias(); // null $result->getRoute(); // null $result->getAllowedMethods(); // array('GET' => true, 'POST' => true) // now with the right method $result = $router->route('GET', '/path/to/content'); // a match with arguments set to the route $result->isEmpty(); // false $result->getAlias(); // null $result->getRoute(); // Route instance $result->getRoute()->getArguments(); // array('action' => 'content'); // what about the alias? $result = $router->route('GET', '/ptc'); // the same match will be generated $result->isEmpty(); // false $result->getAlias(); // null $result->getRoute(); // Route instance $result->getRoute()->getArguments(); // array('action' => 'content'); // let's force the alias $alias->setIsForced(true); // what about the alias now? $result = $router->route('GET', '/ptc'); // still the same $result->isEmpty(); // false $result->getAlias(); // null $result->getRoute(); // Route instance $result->getRoute()->getArguments(); // array('action' => 'content'); // but when we take our original request ... $result = $router->route('GET', '/path/to/content'); // ... we see we need to redirect $result->isEmpty(); // false $result->getAlias(); // Alias instance // let's test multi domain support $route = new Route('/path', 'callback', 'id2'); $route->setBaseUrl('http://some-server.com'); $routeContainer->setRoute($route); $result = $router->route('GET', '/path', 'http://other-server.com'); $result->isEmpty(); // true $result = $router->route('GET', '/path', 'http://some-server.com'); $result->isEmpty(); // false // create some urls // http://some-server.com/path $url = $routeContainer->getUrl('http://my-server.com', 'id2'); // http://my-server.com/ptc $url = $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'content')); // http://my-server.com/path/to/my-action $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'my-action')); // http://my-server.com/path/to/my-action?limit=20&page=1 $url = $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'my-action'), array('page' => 1, 'limit' => 20)); // http://my-server.com/path/to/your-action?limit=20&page=2 $url = $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'my-action'), array('page' => 1, 'limit' => 20), '&'); $url->setArgument('action', 'your-action'); $url->setQueryParameter('page', 2); // translates an URL to it's alias if available and needed $url = $routeContainer->getUrlAlias($url);
实现
对于更多示例,你可以查看以下库的实现
安装
你可以使用Composer来安装此库。
composer require ride/lib-router