hehey-hrouter 是一个python 路由工具组件,仿Yii2 路由规则
https://gitee.com/chinahehe/hehey-hrouter
from hrouter.RouterManager import RouterManager,reg_route_rule
router = RouterManager({
'router':{
#'clazz': 'hehe.core.hrouter.easy.EasyRouter.EasyRouter',# url 路由器,不填,则默认,一般不填
#'routerRequest':'WebRouterRequest',# 路由规则
# url 路由规则定义
'rules':[
#uri 请求地址规则,action 操作地址规则,method 请求方法,clazz 规则类,用于扩展
#{'uri':'/','action':'/index','method'='get','clazz'=>''},
#{'uri':'/','action':'/'},
{'uri':'/.html','action':'/detail'}
],
# action 规则配置
'actionRule':{
'filter': ['site', 'controllers', 'modules'],# 自动过滤包路径的关键词
'suffix': ['Action', 'Controller'],# action 后缀 ['action 方法后缀','Controller 控制器类后缀']
'func':'' # action 地址处理方法
}
}
});
# 注册路由规则-函数
@reg_route_rule('user/getuserlist')
def getuser(self):
print("pass")
return "您好
"
# 注册路由规则-类方法
class NewsController:
@reg_route_rule('news/list')
def getindex(self):
print("pass")
return "您好
"
# 资讯详情
def detail(self):
print("pass")
return "您好
"
pass
# 创建路由request 对象
routerRequest = router.runRoute({'PATH_INFO':"news/list"});
# 获取解析后的路由地址
routeUrl = routerRequest.getRouteUrl() # 获取路由解析后url地址,比如news/index
routeParams = routerRequest.getRouteParams();# 获取路由的解析后参数{"id":1}
# route = news/getindex
print(routeUrl)
# 生成地址
url = router.buidlUrl('news/getindex',{"id":"10"})
# url: news/list?id=10
url = router.buidlUrl('getuser',{"id":"10"})
# url: user/getuserlist?id=10
url = router.buidlUrl('news/detail',{"id":"10"})
# url: news/10.html
print(url)