ke/buildrouter
Composer 安装命令:
composer require ke/buildrouter
包简介
ThinkPHP5.0/5.1的注解路由
README 文档
README
建议使用go编写的新版本 https://github.com/wdaglb/thinkphp-annotation-route
KE-BuileRouter TP注解路由
兼容ThinkPHP 5.0与5.1,不兼容3.x
安装
composer require ke/buildrouter
使用
只需要使用composer安装扩展就自动加载,无需任何其它代码来启动
1.1 版本增加了常量KE_ROUTE_AUTO=0来关闭自动启动(需要在composer/autoload.php前定义)
当TP设置为调试模式时(也就是app_debug为true)时每次请求都会生成route/build_route.php
注意:如果是5.0则会生成application/build_route.php
部署时你应该使用下面命令生成路由表
php think ke-buildrouter
配置(1.1+)
配置需要加在app.php
// 注解路由全局前缀
'ke_route_prefix'=>'',
// 注解路由变量, 定义时变量使用$pre
'ke_route_vars'=>[
'pre'=>'api'
],
注解格式
与TP官方的差不多,目前只支持
@route('rule')
// 如需指定请求类型,传入第二个参数即可,支持路由模式下所有的类型
@route('rule', 'method')
// 使用注解变量
@route('$pre/demo')
在控制器类的方法注释中定义(通常称之为注解路由),例如:
<?php
namespace app\index\controller;
class Index
{
/**
* @param string $name 数据名称
* @return mixed
* @route('hello/:name')
*/
public function hello($name)
{
return 'hello,'.$name;
}
}
请务必注意注释的规范,可以利用IDE生成规范的注释。
该方式定义的路由在调试模式下面实时生效,部署模式则需要使用 php think ke-buildrouter 指令生成路由规则文件。
注意必须严格使用@route((区分大小写,route和(之间不能有空格),建议路由定义写在注释最后一段,否则后面需要一个空行。
默认注册的路由规则是支持所有的请求,如果需要指定请求类型,可以在第二个参数中指定请求类型:
<?php
namespace app\index\controller;
class Index
{
/**
* @param string $name 数据名称
* @return mixed
* @route('hello/:name','get')
*/
public function hello($name)
{
return 'hello,'.$name;
}
}
然后就使用下面的URL地址访问:
http://tp5.com/hello/thinkphp
页面输出
hello,thinkphp
关于URL生成
建议使用url函数生成
// application/index/controller/Index.php
url('index/Index/index')
// application/index/controller/user/Message.php
url('index/user.Message/index')
// application/index/controller/UserMessage.php
url('index/UserMessage/index')
这里借用了官方文档
后续可能支持option参数的设置?
ke/buildrouter 适用场景与选型建议
ke/buildrouter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 118 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「router」 「annotation」 「thinkphp」 「think」 「annotation route」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ke/buildrouter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ke/buildrouter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ke/buildrouter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
Symfony bundle to add arbitrary metadata to classes and their constants by annotation
A Laravel-like router for the WordPress Rewrite API
A Laravel helper to detect if the current route/path is active.
Router
统计信息
- 总下载量: 118
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2018-12-12