fyre/router

一个URL路由库。

v5.1.3 2024-08-18 02:42 UTC

README

FyreRouter 是一个免费的、开源的 PHP URL 路由库。

目录

安装

使用 Composer

composer require fyre/router

在 PHP 中

use Fyre\Router\Router;

方法

添加占位符

添加一个占位符。

  • $placeholder 是表示占位符的字符串。
  • $pattern 是表示正则表达式的字符串。
Router::addPlaceholder($placeholder, $pattern);

清除

清除所有路由和别名。

Router::clear();

连接

连接一个路由。

  • $path 是表示路由路径的字符串,可以包括占位符或正则表达式(将传递给目标)。
  • $destination 可以是表示目标的字符串、包含类名和方法的数组或一个 Closure
  • $options 是包含配置选项的数组。
    • as 是表示路由别名的字符串,默认为 null
    • middleware 是应用于路由的中间件数组,默认为 []
    • method 是表示匹配方法的字符串数组,默认为 []
    • redirect 是表示该路由是否为重定向的布尔值,默认为 false
Router::connect($path, $destination, $options);

您可以为连接特定路由生成以下辅助方法。

Router::delete($path, $destination, $options);
Router::get($path, $destination, $options);
Router::patch($path, $destination, $options);
Router::post($path, $destination, $options);
Router::put($path, $destination, $options);
Router::redirect($path, $destination, $options);

请参阅路由部分以了解支持的目标格式。

获取基本 URI

获取基本 URI。

$baseUri = Router::getBaseUri();

获取占位符

获取占位符。

$placeholders = Router::getPlaceholders();

获取请求

获取服务器请求。

$request = Router::getRequest();

此方法将返回一个 ServerRequest

获取路由

获取已加载的路由。

$route = Router::getRoute();

此方法将返回一个 Route

分组

创建一组路由。

  • $options 是包含分组选项的数组。
    • prefix 是表示路由分组路径前缀的字符串,默认为 null
    • as 是表示路由分组别名前缀的字符串,默认为 null
    • middleware 是应用于路由分组的中间件数组,默认为 []
  • $callback 是一个 Closure,其中可以使用前缀定义路由。
Router::group($options, $callback);

加载路由

加载一个路由。

Router::loadRoute($request);

设置基本 URI

设置基本 URI。

  • $baseUri 是表示基本 URI 的字符串。
Router::getBaseUri($baseUri);

设置请求

设置服务器请求。

Router::setRequest($request);

URL

为命名路由生成一个 URL。

  • $name 是表示路由别名的字符串。
  • $arguments 是包含路由参数的数组。
    • ? 是包含路由查询参数的数组。
    • # 是表示 URI 的片段组件的字符串。
  • $options 是包含路由选项的数组。
    • fullBase 是表示是否使用完整基本 URI 的布尔值,默认为 false
$url = Router::url($name, $arguments, $options)

路由

所有路由都扩展了 Fyre\Router\Route 类,并包括以下方法。

检查方法

检查路由是否与测试方法匹配。

  • $method 是表示要测试的方法的字符串。
$checkMethod = $route->checkMethod($method);

检查路径

检查路由是否匹配测试路径。

  • $path 是表示测试路径的字符串。
$checkPath = $route->checkPath($path);

获取参数

获取路由参数。

$arguments = $route->getArguments();

获取目标

获取路由目标。

$destination = $route->getDestination();

获取中间件

获取路由中间件。

$middleware = $route->getMiddleware();

获取路径

获取路由路径。

$path = $route->getPath();

处理

处理路由。

$response = $route->process($request, $response);

此方法将返回一个 ClientResponse

从路径设置参数

从路径设置路由参数。

  • $path 是表示路径的字符串。
$newRoute = $route->setArgumentsFromPath($path);

设置方法

  • $methods 是包含路由方法的数组。
$newRoute = $route->setMethods($methods);

设置中间件

  • $middleware 是包含路由中间件的数组。
$newRoute = $route->setMiddleware($middleware);

闭包路由

use Fyre\Router\Routes\ClosureRoute;
  • $destination 是一个 Closure
  • $path 是表示路由路径的字符串,默认为 ""。
$route = new ClosureRoute($destination, $path);

$destination 应该用以下格式表示

$destination = function(...$args) {
    return $response;
};

控制器路由

use Fyre\Router\Routes\ControllerRoute;
  • $destination 是包含控制器类名和方法的数组。
  • $path 是表示路由路径的字符串,默认为 ""。
$route = new ControllerRoute($destination, $path);

$destination 可以用以下格式表示

$destination = [MyClass::class]; // defaults to index method
$destination = [MyClass::class, 'method'];

获取操作

获取路由控制器操作。

$action = $route->getAction();

获取控制器

获取路由控制器类名。

$controller = $route->getController();

重定向路由

use Fyre\Router\Routes\RedirectRoute;
  • $destination 是表示目标的字符串。
  • $path 是表示路由路径的字符串,默认为 ""。
$route = new RedirectRoute($destination, $path);

$destination 可以用以下格式表示

$destination = 'https://test.com/';
$destination = 'https://test.com/$1';

路由中间件

use Fyre\Router\Middleware\RouterMiddleware;
$middleware = new RouterMiddleware();

处理

$response = $middleware->process($request, $handler);

此方法将返回一个 ClientResponse