mnavarrocarter/path-to-regexp-php

Path-To-Regex JS的PHP版本

1.0.0 2020-02-08 20:15 UTC

This package is auto-updated.

Last update: 2024-09-09 06:08:52 UTC


README

Actions Status

将Express风格的路径字符串(如 /user/:name)转换为正则表达式,以便在路由引擎中使用。

这是著名的JS库path-to-regexp的对象化版本,由Node的Express和其他JS框架使用。

将JS库移植到PHP的艰巨工作最初由Gil Polguère完成。他完成了艰难的部分。

我添加了以下功能

  1. 将PHP版本提升到7.2最低版本
  2. 在可能的情况下添加了类型提示
  3. 移除了所有通过引用传递的数组,并使用对象来存储该状态。

用法

use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/user/:name');
$result = $pathRegex->match('/user/john');
$result->getMatchedString();    // '/user/john'
$result->getValues();           // ['name' => 'john']

您可以在create方法的选项中传递多个标志

  • PathRegExpFactory::CASE_SENSITIVE:当传递时,路由将被视为大小写敏感。
  • PathRegExpFactory::STRICT:当传递时,路径末尾允许有斜杠。
  • PathRegExpFactory::END:当传递时,路径将匹配开始位置。

默认情况下,create方法中启用的唯一标志是PathRegExpFactory::END

参数

路径可以定义参数并自动填充键数组。

命名参数

命名参数通过在参数名前加冒号(:foo)来定义。默认情况下,此参数将匹配到下一个路径段。

use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo/:bar');
$pathRegex->getParts()[0]->getName(); // 'foo'
$pathRegex->getParts()[1]->getName(); // 'bar'

$result = $pathRegex->match('/test/route');
$result->getMatchedString();    // '/test/route'
$result->getValues();           // ['foo' => 'test', 'bar' => 'route']

后缀参数

可选

参数可以通过在末尾加问号(?)使其整个参数可选。这也会使任何前缀路径分隔符(/.)可选。

use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo/:bar?');

$result = $pathRegex->match('/test');
$result->getMatchedString();    // '/test'
$result->getValues();           // ['foo' => 'test', 'bar' => null]

$result = $pathRegex->match('/test/route');
$result->getMatchedString();    // '/test/route'
$result->getValues();           // ['foo' => 'test', 'bar' => 'route']
零个或多个

参数可以通过在末尾加星号(*)来表示零个或多个参数匹配。前缀路径分隔符也考虑在内。

use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo*');

$result = $pathRegex->match('/');
$result->getMatchedString();    // '/'
$result->getValues();           // ['foo' => null];

$result = $pathRegex->match('/bar/baz');
$result->getMatchedString();    // '/bar/baz'
$result->getValues();           // ['foo' => 'bar/baz']
一个或多个

参数可以通过在末尾加加号(+)来表示一个或多个参数匹配。前缀路径分隔符包含在匹配中。

use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo+');

$pathRegex->match('/'); // Will throw NoMatchException

$result = $pathRegex->match('/bar/baz');
$result->getMatchedString();    // '/bar/baz'
$result->getValues();           // ['foo' => 'bar/baz']

自定义匹配参数

所有参数都可以提供一个自定义匹配正则表达式并覆盖默认值。

请注意:字符串中的反斜杠需要转义。

use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo(\\d+)');

$result = $pathRegex->match('/123');
$result->getMatchedString();    // '/123'
$result->getValues();           // ['foo' => '123']

$pathRegex->match('/abc'); // Will throw a NoMatchException

未命名参数

可以编写一个未命名的参数,它仅是一个匹配组。它的工作方式与命名参数相同,但它将被数字索引。

use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo/(.*)');

$result = $pathRegex->match('/test/route');
$result->getMatchedString();   // '/test/route'
$result->getValues();          // ['foo' => 'test', '0' => 'route']