Home Backend Development PHP Tutorial Tutorial on using PHP routing library FastRoute

Tutorial on using PHP routing library FastRoute

Apr 30, 2020 am 10:00 AM
php

GitHub: https://github.com/nikic/FastRoute

这个库提供了基于正则表达式的快速路由实现。这篇文章解释了 FastRoute 是如何工作的和它为什么很快。

安装

通过 composer 安装

composer require nikic/fast-route

要求 PHP 5.4 及更高的版本

使用

这是一个基本的使用示例

<?php
require &#39;/path/to/vendor/autoload.php&#39;;
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute(&#39;GET&#39;, &#39;/users&#39;, &#39;get_all_users_handler&#39;);
    // {id} 必须是一个数字 (\d+)
    $r->addRoute(&#39;GET&#39;, &#39;/user/{id:\d+}&#39;, &#39;get_user_handler&#39;);
    //  /{title} 后缀是可选的
    $r->addRoute(&#39;GET&#39;, &#39;/articles/{id:\d+}[/{title}]&#39;, &#39;get_article_handler&#39;);
});
// 获取请求的方法和 URI
$httpMethod = $_SERVER[&#39;REQUEST_METHOD&#39;];
$uri = $_SERVER[&#39;REQUEST_URI&#39;];
// 去除查询字符串( ? 后面的内容) 和 解码 URI
if (false !== $pos = strpos($uri, &#39;?&#39;)) {
    $uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);
$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::NOT_FOUND:
        // ... 404 Not Found 没找到对应的方法
        break;
    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        $allowedMethods = $routeInfo[1];
        // ... 405 Method Not Allowed  方法不允许
        break;
    case FastRoute\Dispatcher::FOUND: // 找到对应的方法
        $handler = $routeInfo[1]; // 获得处理函数
        $vars = $routeInfo[2]; // 获取请求参数
        // ... call $handler with $vars // 调用处理函数
        break;
}
Copy after login

定义路由

通过调用 FastRoute\simpleDispatcher() 函数来定义路由,该函数接受一个以 FastRoute\RouteCollector 实例为参数的闭包作为参数。通过在 collector 实例里面调用 addRoute() 增加路由。

$r->addRoute($method, $routePattern, $handler);
Copy after login

$method 是大写的 HTTP 方法,能够被某个路由匹配,可以使用数组指定多个有效的 $method

// 这里两行调用
$r->addRoute(&#39;GET&#39;, &#39;/test&#39;, &#39;handler&#39;);
$r->addRoute(&#39;POST&#39;, &#39;/test&#39;, &#39;handler&#39;);
// 等同于这一行调用
$r->addRoute([&#39;GET&#39;, &#39;POST&#39;], &#39;/test&#39;, &#39;handler&#39;);
Copy after login

默认情况下 $routePattern 使用一种语法,比如 {foo} 是指定名称为 foo 的占位符,可以匹配正则表达式 [^/]+. 。要调整占位符匹配的模式,可以通过编写 {bar:[0-9] +} 来指定自定义模式。一些例子

// 匹配 /user/42,不匹配 /user/xyx
$r->addRoute(&#39;GET&#39;, &#39;/user/{id:\d+}&#39;, &#39;handler&#39;);
// 匹配 /user/foobar,不匹配 /user/foo/bar
$r->addRoute(&#39;GET&#39;, &#39;/user/{name}&#39;, &#39;handler&#39;);
// 匹配 /user/foobar,也匹配 /user/foo/bar
$r->addRoute(&#39;GET&#39;, &#39;/user/{name:.+}&#39;, &#39;handler&#39;);
Copy after login

路由占位符的自定义模式不能使用捕获组,例如 {lang:(en|de)} 不是有效的占位符,因为 () 是一个捕获组,可以使用 {lang:en|de} 或者 {lang:(?:en|de)} 代替。

另外,在路由 [...] 中定义的部分是可选匹配的,所以 /foo[bar] 将匹配 /foo 和 /foobar 。路由可选部分只支持在定义的末尾,而不能在定义的中间。

// 这个路由有,[/{name}] 可选择匹配部分
$r->addRoute(&#39;GET&#39;, &#39;/user/{id:\d+}[/{name}]&#39;, &#39;handler&#39;);
// 等同于这两个路由
$r->addRoute(&#39;GET&#39;, &#39;/user/{id:\d+}&#39;, &#39;handler&#39;);
$r->addRoute(&#39;GET&#39;, &#39;/user/{id:\d+}/{name}&#39;, &#39;handler&#39;);
// 多层嵌套可选路由,也是支持的
$r->addRoute(&#39;GET&#39;, &#39;/user[/{id:\d+}[/{name}]]&#39;, &#39;handler&#39;);
// 这个路由定义无效,因为可选部分只能在定义的末尾
$r->addRoute(&#39;GET&#39;, &#39;/user[/{id:\d+}]/{name}&#39;, &#39;handler&#39;);
Copy after login

$handler 参数不一定必须是回调函数,它也可以是控制器类名或任何其他类型的数据。FastRoute 只告诉你哪个 handler 对应 URI,如何解释它取决于你。

请求方法的书写快捷方式

对于 GETPOSTPUTPATCHDELETE HEAD 请求方法,可使用快捷方式。

$r->get(&#39;/get-route&#39;, &#39;get_handler&#39;);
$r->post(&#39;/post-route&#39;, &#39;post_handler&#39;);
// 等同于
$r->addRoute(&#39;GET&#39;, &#39;/get-route&#39;, &#39;get_handler&#39;);
$r->addRoute(&#39;POST&#39;, &#39;/post-route&#39;, &#39;post_handler&#39;);
Copy after login

路由组

你可以在一个组内定义路由,同一组内的路由有相同的前缀。

$r->addGroup(&#39;/admin&#39;, function (RouteCollector $r) {
    $r->addRoute(&#39;GET&#39;, &#39;/do-something&#39;, &#39;handler&#39;);
    $r->addRoute(&#39;GET&#39;, &#39;/do-another-thing&#39;, &#39;handler&#39;);
    $r->addRoute(&#39;GET&#39;, &#39;/do-something-else&#39;, &#39;handler&#39;);
});
// 等同于
$r->addRoute(&#39;GET&#39;, &#39;/admin/do-something&#39;, &#39;handler&#39;);
$r->addRoute(&#39;GET&#39;, &#39;/admin/do-another-thing&#39;, &#39;handler&#39;);
$r->addRoute(&#39;GET&#39;, &#39;/admin/do-something-else&#39;, &#39;handler&#39;);
Copy after login

可以定义多层嵌套组结构。

缓存

使用 simpleDispatcher 定义路由的回调函数可以无缝缓存。通过使用 cachedDispatcher 而不是 simpleDispatcher,可以缓存生成的路由数据并从缓存的信息构建调度。

<?php
$dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute(&#39;GET&#39;, &#39;/user/{name}/{id:[0-9]+}&#39;, &#39;handler0&#39;);
    $r->addRoute(&#39;GET&#39;, &#39;/user/{id:[0-9]+}&#39;, &#39;handler1&#39;);
    $r->addRoute(&#39;GET&#39;, &#39;/user/{name}&#39;, &#39;handler2&#39;);
}, [
    &#39;cacheFile&#39; => __DIR__ . &#39;/route.cache&#39;, /* required 缓存文件路径,必须设置 */
    &#39;cacheDisabled&#39; => IS_DEBUG_ENABLED,     /* optional, enabled by default 是否缓存,可选参数,默认情况下开启 */
]);
Copy after login

该函数的第二个参数是一个选项数组,可用于指定缓存文件路径等等。

调度 URI

通过调用 dispatch() 调度 URI。这个方法接受 HTTP 方法 和一个 URI 作为参数。获得这两个信息是你自己的工作,这个库并不绑定到 PHP web SAPIs 。

dispatch() 返回一个数组,第一个元素是一个状态码,状态码是 Dispatcher::NOT_FOUNDDispatcher::METHOD_NOT_ALLOWEDDispatcher::FOUND 其中之一。对于 Dispatcher::METHOD_NOT_ALLOWED 状态,第二个数组元素包含允许提供的 URI 的 HTTP 方法列表。

[FastRoute\Dispatcher::METHOD_NOT_ALLOWED, [&#39;GET&#39;, &#39;POST&#39;]]
Copy after login

对于 Dispatcher::FOUND 状态,第二个数组元素是 $handler ,第三个数组元素是是一个包含所有占位符的数组

/* Routing against GET /user/nikic/42 */
[FastRoute\Dispatcher::FOUND, &#39;handler0&#39;, [&#39;name&#39; => &#39;nikic&#39;, &#39;id&#39; => &#39;42&#39;]]
Copy after login

重写路由解析器和调度器

这个库使用三个组件,一个路由解析器,一个数据生成器,一个调度器。这个三个组件实现以下接口

<?php
namespace FastRoute;
interface RouteParser {
    public function parse($route);
}
interface DataGenerator {
    public function addRoute($httpMethod, $routeData, $handler);
    public function getData();
}
interface Dispatcher {
    const NOT_FOUND = 0, FOUND = 1, METHOD_NOT_ALLOWED = 2;
    public function dispatch($httpMethod, $uri);
}
Copy after login

路由解析器获取路由模式字符串并将其转换为路由信息数组,其中每个路线信息又是它的部分数组。

/* The route /user/{id:\d+}[/{name}] converts to the following array: */
[
    [
        &#39;/user/&#39;,
        [&#39;id&#39;, &#39;\d+&#39;],
    ],
    [
        &#39;/user/&#39;,
        [&#39;id&#39;, &#39;\d+&#39;],
        &#39;/&#39;,
        [&#39;name&#39;, &#39;[^/]+&#39;],
    ],
]
Copy after login

然后可以将该数组传递给数据生成器的 addRoute() 方法,在添加了所有路由之后,调用生成器的 getData(),它将返回调度器所需的所有路由数据。

调度程序通过构造函数接受路由数据,并提供 dispatch()方法。

路由解析器可以被单独覆盖,然而数据生成器和调度器应该总是一起修改,因为前者的输出与后者的输入紧密耦合。

当使用 simpleDispatcher / cachedDispatcher 时,可以通过传入额外的参数,进行覆盖

<?php
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    /* ... */
}, [
    &#39;routeParser&#39; => &#39;FastRoute\\RouteParser\\Std&#39;,
    &#39;dataGenerator&#39; => &#39;FastRoute\\DataGenerator\\GroupCountBased&#39;,
    &#39;dispatcher&#39; => &#39;FastRoute\\Dispatcher\\GroupCountBased&#39;,
]);
Copy after login

上面给出了默认的设置,通过把 GroupCountBased 替换成 GroupPosBased 可以使用完全不同的调度策略

关于HEAD请求的说明

HTTP 规范要求服务器 同时支持 GET HEAD 方法

GETHEAD方法必须得到所有通用服务器的支持

To avoid forcing the user to manually register a HEAD route for each resource, the request will be responded to with a matching GET route. PHP web SAPI transparently removes the entity body from the HEAD response, so this behavior has no impact on the vast majority of users.

However, when using FastRoute outside a Web SAPI environment, you must never send the entity body generated in response to a HEAD request. If you are a non-SAPI user, this is your responsibility; in this case However, FastRoute has no power to restrict you from breaking HTTP.

Finally, note that an application can always specify its own HEAD method route for a given resource to bypass this behavior entirely.

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of Tutorial on using PHP routing library FastRoute. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles