mnavarrocarter/path-to-regexp-php
Composer 安装命令:
composer require mnavarrocarter/path-to-regexp-php
包简介
A PHP port of Path-To-Regex JS
README 文档
README
Turns an Express-style path string such as /user/:name into a regular expression,
so it can be used in routing engines.
This is an Object Oriented port of the famous JS library path-to-regexp, used by Node's Express
and other js frameworks.
The hard work of porting the JS library to PHP was done originally done by Gil Polguère. He did the hard part.
I just added the following features:
- Bumped PHP to 7.2 minimum
- Added type hints where possible
- Removed all the arrays passed by reference and used objects to store that state instead.
Usage
use MNC\PathToRegExpPHP\PathRegExpFactory; $pathRegex = PathRegExpFactory::create('/user/:name'); $result = $pathRegex->match('/user/john'); $result->getMatchedString(); // '/user/john' $result->getValues(); // ['name' => 'john']
You have several flags you can pass as options of the create method:
PathRegExpFactory::CASE_SENSITIVE: When passed the route will be treated as case sensitive.PathRegExpFactory::STRICT: When passed a slash is allowed to be trailing the path.PathRegExpFactory::END: When not passed the path will match at the beginning.
By default, the only flag enabled in the create method is PathRegExpFactory::END.
Parameters
The path has the ability to define parameters and automatically populate the keys array.
Named Parameters
Named parameters are defined by prefixing a colon to the parameter name (:foo).
By default, this parameter will match up to the next path segment.
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']
Suffixed Parameters
Optional
Parameters can be suffixed with a question mark (?) to make the entire parameter optional.
This will also make any prefixed path delimiter optional (/ or .).
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']
Zero or more
Parameters can be suffixed with an asterisk (*) to denote a zero or more parameter match.
The prefixed path delimiter is also taken into account for the match.
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']
One or more
Parameters can be suffixed with a plus sign (+) to denote a one or more parameters match.
The prefixed path delimiter is included in the match.
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']
Custom Match Parameters
All parameters can be provided a custom matching regexp and override the default.
Please note: Backslashes need to be escaped in strings.
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
Unnamed Parameters
It is possible to write an unnamed parameter that is only a matching group. It works the same as a named parameter, except it will be numerically indexed.
use MNC\PathToRegExpPHP\PathRegExpFactory; $pathRegex = PathRegExpFactory::create('/:foo/(.*)'); $result = $pathRegex->match('/test/route'); $result->getMatchedString(); // '/test/route' $result->getValues(); // ['foo' => 'test', '0' => 'route']
mnavarrocarter/path-to-regexp-php 适用场景与选型建议
mnavarrocarter/path-to-regexp-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.11k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「routing」 「path」 「convert」 「Match」 「regexp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mnavarrocarter/path-to-regexp-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mnavarrocarter/path-to-regexp-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mnavarrocarter/path-to-regexp-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
JSONPath implementation for querying and updating JSON objects with support for json flattening into a table
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
The InstantConfigurationCopy module provides easy way to copy configuration field information for admin in back office Magento 2.
A Laravel-like router for the WordPress Rewrite API
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
统计信息
- 总下载量: 1.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-08