slince/routing

灵活的Web路由组件

1.0.0 2017-05-25 04:03 UTC

This package is auto-updated.

Last update: 2024-09-08 17:30:28 UTC


README

Build Status Coverage Status Latest Stable Version Scrutinizer

灵活的Web路由组件。

安装

通过Composer安装

composer require slince/routing

快速示例

$routes = new Slince\Routing\RouteCollection();
$routes->get('/products', 'Products::index')->setName('product_index');

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(); //Creates the psr7 request instance

$matcher = new Slince\Routing\Matcher($routes);
$generator = new Slince\Routing\Generator($request);

$route = $matcher->matchRequest($request); //Matches the current request
var_dump($route->getComputedParamters()); //Dumps route computed paramters
echo $generator->generate($route); //Generates path 

$route = $routes->getByAction('Products::index');
echo $generator->generate($route); //Generates path 

$route = $routes->getByName('product_index');
echo $generator->generate($route); //Generates path 

用法

定义路由

首先创建一个 Slince\Routing\RouteCollection 实例,

$routes = new Slince\Routing\RouteCollection();
$route = new Slince\Routing\Route('/products/{id}', 'Products::view');
$routes->add($route);

路由路径包含占位符 {id},它可以匹配除 "/" 和 "." 之外的所有内容。您可以使用 setRequirement 方法或 setRequirements 方法设置自定义要求。

$route->setRequirements([
    'id' => '\d+'
]);

路由支持可选的占位符,您可以为占位符提供一个默认值。

$route->setDefaults([
    'id' => 1
]);

该路由可以匹配 /products/products/1

提供HTTP方法的缩写。

$routes = new RouteCollection();

$routes->get('/pattern', 'action');
$routes->post('/pattern', 'action');
$routes->put('/pattern', 'action');
$routes->delete('/pattern', 'action');
$routes->options('/pattern', 'action');
$routes->patch('/pattern', 'action');

自定义HTTP动词

$route->setMethods(['GET', 'POST', 'PUT']);

主机匹配

您可以使用 setHost 方法将路由限制为指定的主机。

$routes->create('/products', 'Products::index')
    ->setHost('product.domain.com');

该路由将仅匹配具有 product.domain.com 域的请求

强制路由使用HTTPS或HTTP

路由还允许您使用 httphttps 定义路由。

$routes = new Slince\Routing\RouteCollection();

$routes->https('/pattern', 'action');
$routes->http('/pattern', 'action');

或自定义此设置。

$route->setSchemes(['http', 'https']);

匹配路径或psr7请求。

$routes = new Slince\Routing\RouteCollection();
$routes->create('/products/{id}.{_format}', 'Products::view');
$matcher = new Slince\Routing\Matcher($routes);

try {
    $route = $matcher->match('/products/10.html');
    
    print_r($route->getComputedParameters())// ['id' => 10, '_format' => 'html']
    
} catch (Slince\Routing\Exception\RouteNotFoundException $e) {
    //404
}

匹配器将返回匹配的路由。如果找不到匹配的路由,匹配器将抛出 RouteNotFoundException

匹配 Psr\Http\Message\ServerRequestInterface

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
try {
    $route = $matcher->matchRequest($request);
} catch (Slince\Routing\Exception\MethodNotAllowedException $e) {
    //403
    var_dump($e->getAllowedMethods());
} catch (Slince\Routing\Exception\RouteNotFoundException $e) {
    //404
}

为路由生成路径

$generator = new Slince\Routing\Generator();

$route = new Slince\Routing\Route('/foo/{id}', 'action');
echo $generator->generate($route, ['id' => 10]); //will output "/foo/10"

如果您想为路由生成绝对URL,则需要将请求作为请求上下文提供给生成器。

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
$generator->setRequest($request);

echo $generator->generate($route, ['id' => 10], true); //will output "{scheme}://{domain}/foo/10"

许可证

MIT许可证。请参阅 MIT