Download the PHP package guirong/php-router without Composer

On this page you can find all versions of the php package guirong/php-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-router

php simple router

非常快速且轻量的请求匹配路由器。

多个版本:

不同的版本有稍微的区别以适应不同的场景

内置调度器:

路由器管理

RouterManager 当需要在一个项目里处理多个域名下的请求时,方便的根据不同域名配置多个路由器

EN README

项目地址

安装

压测

自动生成了1000条路由,每条有9个参数位,分别测试1000次的

Worst-case matching

This benchmark matches the last route and unknown route. It generates a randomly prefixed and suffixed route in an attempt to thwart any optimization. 1,000 routes each with 9 arguments.

This benchmark consists of 14 tests. Each test is executed 1,000 times, the results pruned, and then averaged. Values that fall outside of 3 standard deviations of the mean are discarded.

Test Name Results Time(ms) + Interval Change
inhere/sroute(Router) - unknown route(1000 routes) 990 0.002031 +0.000871 75% slower
inhere/sroute(SRouter) - unknown route(1000 routes) 994 0.002895 +0.001736 150% slower
inhere/sroute(Router) - last route(1000 routes) 997 0.005300 +0.004141 357% slower
inhere/sroute(SRouter) - last route(1000 routes) 997 0.006467 +0.005308 458% slower
symfony/routing(cached) - unknown route(1000 routes) 976 0.012777 +0.011618 1002% slower
symfony/routing(cached) - last route(1000 routes) 996 0.013608 +0.012449 1074% slower
mindplay/timber - last route(1000 routes) 998 0.017211 +0.016052 1385% slower
FastRoute - unknown route(1000 routes) 991 0.039429 +0.038270 3302% slower
FastRoute(cached) - unknown route(1000 routes) 990 0.040800 +0.039641 3420% slower
FastRoute(cached) - last route(1000 routes) 999 0.045065 +0.043906 3788% slower
FastRoute - last route(1000 routes) 999 0.064694 +0.063535 5481% slower
Pux PHP - unknown route(1000 routes) 978 0.316016 +0.314857 27163% slower
symfony/routing - unknown route(1000 routes) 992 0.359482 +0.358323 30912% slower
symfony/routing - last route(1000 routes) 999 0.418813 +0.417654 36031% slower
Pux PHP - last route(1000 routes) 999 0.440489 +0.439330 37901% slower
Macaw - unknown route(1000 routes) 991 1.687441 +1.686282 145475% slower
Macaw - last route(1000 routes) 999 1.786542 +1.785383 154024% slower

First route matching

This benchmark tests how quickly each router can match the first route. 1,000 routes each with 9 arguments.

This benchmark consists of 7 tests. Each test is executed 1,000 times, the results pruned, and then averaged. Values that fall outside of 3 standard deviations of the mean are discarded.

Test Name Results Time + Interval Change
nikic/fast-route - first route(1000) 998 0.002929 +0.001571 116% slower
corneltek/pux(php) - first route(1000) 996 0.002971 +0.001613 119% slower
inhere/sroute(Router) - first(1000) 979 0.006202 +0.004844 357% slower
inhere/sroute(SRouter) - first(1000) 999 0.006627 +0.005269 388% slower
symfony/routing(cached) - first route(1000) 985 0.006858 +0.005501 405% slower
symfony/routing - first route(1000) 995 0.023105 +0.021747 1601% slower
nikic/fast-route(cached) - first route(1000) 999 0.041133 +0.039775 2929% slower
Macaw - first route (1000 routes) 999 1.782017 +1.780659 131128% slower

使用说明

各个版本的方法名和参数都是一样的

首先, 需要导入类

快速开始

创建一个简单的 public/index.php 文件:

使用php启动一个测试server: php -S 127.0.0.1:8080 -t ./public

好了,现在你可以访问 http://127.0.0.1:8080 可以看到输出 hello

如果是直接下载的包代码,可以加载 test/boot.php 文件,也可以加载到 Guirong\PhpRouter 命名空间.

用如下的语句替换上面的 autoload.php 加载语句即可:

添加路由

使用路由组

使用控制器

备用路由处理

可以注册一个备用路由处理。当没匹配到时,就会使用它

如果配置了 'ignoreLastSlash' => true, '/index' 等同于 '/index/'

注意

可选参数 - 只能是在路由path的最后

正确的:

错误的:

自动匹配路由

支持根据请求的URI自动匹配路由(就像 yii 一样), 需配置 autoRoute.

请参看示例 example 中的使用

此时请求没有配置路由的 /demo /demo/test。将会自动尝试从 App\\Controllers 命名空间下去查找 DemoController

查找逻辑是

设置路由配置

NOTICE: 必须在添加路由之前调用 $router->config()

路由匹配

示例

根据请求的 URI path 和 请求 METHOD 查找匹配我们定义的路由信息。

根据返回的路由信息,我们就可以自由的决定如何调用对应的处理。

关于返回的数据结构,可以查看 关键方法参考

路由调度

如果你不想自己实现路由调度,可以使用内置的路由调度器 Guirong\PhpRouter\Dispatcher\Dispatcher

设置事件处理(可选)

开始路由匹配和调度

使用控制器方法

通过@符号连接控制器类和方法名可以指定执行方法。

NOTICE: 若第二个参数仅仅是个 类,将会尝试执行通过 defaultAction 配置的默认方法

动态匹配控制器方法

动态匹配控制器方法, 需配置

NOTICE: 使用动态匹配控制器方法, 应当使用 any() 添加路由. 即此时不能限定请求方法 REQUEST_METHOD

NOTICE: 上面两个的区别是 第一个无法匹配 /home

使用方法执行器

配置 actionExecutor 为你需要的方法名,例如配置为 'actionExecutor' => 'run',那所有的方法请求都会提交给此方法。 会将真实的 action 作为参数传入run($action), 需要你在此方法中调度来执行真正的请求方法。

NOTICE: 在你需要将路由器整合到自己的框架时很有用

示例:

开始路由匹配和调度

中间件(可选)

中间件提供了一种方便的机制来检查和过滤进入应用程序的 HTTP 请求。

NOTICE: 以下中间件将在应用程序处理请求之前执行一些任务:

NOTICE: 此中间件将在请求由应用程序处理后执行其任务:

NOTICE: 定义中间件配置类

如果您想自己定义配置类,请先继承默认配置 Guirong\PhpRouter\Middleware\Config

NOTICE: 全局中间件默认全局生效,路由中间需手动指派

示例:

在单一路由中启用

在路由组中启用

路由匹配和调度

中间件异常捕获

配置完中间件后,所有系统异常将被$response捕获,并挂载至 exception 属性中,您可以通过 $response->getException() 获取异常,自行处理

NOTICE: 在以上路由中配置完中间件,http 请求会经过 before 中间件的 handle 函数,在处理完主业务后,继而经过 after 中间件的 handle 函数,最后在程序 response 后依次进入 before ,after 中间件的 terminate 函数

运行示例

示例代码在 example 下。

你可以通过 php -S 127.0.0.1:5670 example/object.php 来运行一个测试服务器, 现在你可以访问 http://127.0.0.1:5671

测试

License

MIT

我的其他项目

guirong/cli-message github

一个简单易用的,命令行输出样式工具库


All versions of php-router with dependencies

PHP Build Version
Package Version
Requires php Version >7.1.0
guirong/php-closure Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package guirong/php-router contains the following files

Loading the files please wait ....