定制 bephp/router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bephp/router

Composer 安装命令:

composer require bephp/router

包简介

A fast router for PHP. It matches urls and executes PHP functions. automatic get variable based on handler function parameter list. Suport to compile router callback handlers into plain array source code.

README 文档

README

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License
A barebones router for PHP.
It matches urls and executes PHP functions.
Automatic get variable based on handler function parameter list.
Suport to compile router callback handlers into plain array source code.

中文版.

Installation

composer require bephp/router

API Reference

group/prefix($prefix, $hook)

add group routers with same prefix. if not pass param $prefix just reset attribute prefix of router instance to empty string. will merge the $hook to this group.

match($method, $path, $callback, $hook)

create the router tree based on given $method and $path, the $callback and $hook will stored in the leaf node.

get/post/put/delete/head/options($path, $callback, $hook)

wrap the match method without $method parameter. also defined "post", "put", "delete", "head" and so on.

execute()

the enter point of the application. have 3 optional parameters, $params will be merged with request variable and passed into callback handler. can pass $method and $path when not deploy as web server, can using the 2 parameters to test this library.

error()

  1. if call this API with $error_code and $callback, just define the callback handler for the error code.
  2. if call this API with $error_code and other parameters, will trigger the error callback handler, the parameters will passed to the error handler.

hook()

  1. if call this API with $hook_name and $callback, just define the callback handler for the hook name.
  2. if call this API with $hook_name and other parameters, will trigger the hook handler, the parameters will passed to the hook handler.
  3. there's 2 spec hook: "before" and "after", this library will auto call "before" hook before execute the handler, and call "after" hook after execute the handler.
  4. the "after" hook will auto trigger with the return value of callback handler.
  5. the "before" hook, and other user define hooks will auto trigger with the current router object $router, and the merged params stored in $router->params. if these hook return false, will trigger 406 error handler.

Validate

using ctype functions to validate params in pathinfo
example:

if defined router: "/hello/:name:a.json", and using URL: "/hello/lloyd.json" to resolve url.
will call function "ctype_alpha" to validate "lloyd".
validate command to map the ctype functions:

A => ctype_alnum
a => ctype_alpha
d => ctype_digit
x => ctype_xdigit
l => ctype_lower
u => ctype_upper

Compile

the PHP request always match the callback handlers every time. but the request just match one callback. so we can compile the routed tree node into plain array, to save time.

DEV model

using CRouter instead of Router, will always compile the source code into target file.

$crouter = new CRouter("router.inc.php", true);

PRODUCTION model

just include the target source code, and execute it with parameters.

$router = include("router.inc.php");
$router->execute();

Performance

  1. using tree struct to stored callback handler on leaf node. Ensure that the time complexity of find callback function is O(log n). Tree Node
  2. using CRouter class, suport to compile router callback handlers into plain array source code. so can save time to create tree node to store callback by split pathinfo.

Benchmark

using "php-router-benchmark" to test router performance.

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 10 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
Router - unknown route (1000 routes) 993 0.0000232719 +0.0000000000 baseline
Router - last route (1000 routes) 981 0.0000955424 +0.0000722705 311% slower
FastRoute - unknown route (1000 routes) 990 0.0005051955 +0.0004819236 2071% slower
FastRoute - last route (1000 routes) 998 0.0005567203 +0.0005334484 2292% slower
Symfony2 Dumped - unknown route (1000 routes) 998 0.0006116139 +0.0005883420 2528% slower
Symfony2 Dumped - last route (1000 routes) 998 0.0007765370 +0.0007532651 3237% slower
Symfony2 - unknown route (1000 routes) 996 0.0028456177 +0.0028223458 12128% slower
Symfony2 - last route (1000 routes) 993 0.0030129542 +0.0029896823 12847% slower
Aura v2 - last route (1000 routes) 989 0.1707107230 +0.1706874511 733450% slower
Aura v2 - unknown route (1000 routes) 988 0.1798588730 +0.1798356011 772760% slower

Example

belong is one simple example, see the full examples in example.php.

(new Router())
->error(405, function($message){
    header('Location: /hello/world', true, 302);
})
->get('/hello/:name', function($name){
    echo "Hello $name !!!";
})

Start server

php -S 0.0.0.0:8888 example.php

Test

Url not match, trigger 405 error handler.

curl -vvv 127.0.0.1:8888
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:8888
> Accept: */*
> 
< HTTP/1.1 302 Found
< Host: 127.0.0.1:8888
< Connection: close
< X-Powered-By: PHP/5.5.9-1ubuntu4.12
< Location: /hello/world
< Content-type: text/html
< 
* Closing connection 0

Url match get current result.

curl -vvv 127.0.0.1:8888/hello/lloyd
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> GET /hello/lloyd HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:8888
> Accept: */*
> 
< HTTP/1.1 200 OK
< Host: 127.0.0.1:8888
< Connection: close
< X-Powered-By: PHP/5.5.9-1ubuntu4.12
< Content-type: text/html
< 
* Closing connection 0
Hello lloyd !!!

Demo

there's one blog demo, work with ActiveRecord and MicroTpl.

bephp/router 适用场景与选型建议

bephp/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.09k 次下载、GitHub Stars 达 160, 最近一次更新时间为 2015 年 10 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「library」 「micro」 「router」 「fast」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bephp/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bephp/router 我们能提供哪些服务?
定制开发 / 二次开发

基于 bephp/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.09k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 161
  • 点击次数: 19
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 160
  • Watchers: 19
  • Forks: 45
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-22