Package: hehey-hrouter
hehey-hrouter介绍hehey-hrouter 是一个python 路由工具组件,仿Yii2 路由规则依赖以及版本要求python >= 3.5安装直接下载:命令安装：pip install hehey-hrouter基础文件以目录参数配置conf={# 路由规则配置'customRouter':{'clazz':'easy','rules':[#{'uri':'index/user','action':'index/getindexAction'},{'uri':'<controller:\w+>/<action:\w+>','action':'<controller>/<action>'},# {'uri': '<controller:\w+>/<action:\w+>', 'action': '<controller>/<action>'}],'actionRule':{'filter':['site','controllers','modules'],'suffix':['Action','Controller'],'func':''#action 地址处理方法}},# 解析请求的类设置,提供web(WebRouterRequest),命令行解析(ConsoleRouterRequest)'routerRequest':"WebRouterRequest",# 默认为web 请求}customRouter:路由规则配置rules:路由规则actionRule: action 地址规则,提供自动过滤后缀,以及过滤类路径等操作routerRequest:解析请求的类设置,提供web(WebRouterRequest),命令行解析(ConsoleRouterRequest)基本示例快速使用fromhrouter.routeimportRouterManagerconf={'customRouter':{'clazz':'easy','rules':[],'actionRule':{'filter':['site','controllers','modules'],'suffix':['Action','Controller'],'func':''#action 地址处理方法}}}routerManager=RouterManager(**conf);# 解析请求routerRequest=routerManager.runRoute({'PATH_INFO':"news/list"});routeUrl=routerRequest.getRouteUrl()# 获取路由解析后url地址,比如news/indexrouteParams=routerRequest.getRouteParams();# 获取路由的解析后参数{"id":1}# 生成url 地址url=routerManager.buildUrl('news/detail',{"id":"10"})# url: news/detail?id=10解析web请求地址# 浏览器输入:http://xxx.cn/news/index?id=1fromhrouter.routeimportRouterManagerconf={'customRouter':{'clazz':'easy','rules':[],'actionRule':{'filter':['site','controllers','modules'],'suffix':['Action','Controller'],'func':''#action 地址处理方法}},'routerRequest':"WebRouterRequest",# 默认为web 请求}routerManager=RouterManager(**conf);# 解析请求environ={};# uwsgi environ 上下文routerRequest=routerManager.runRoute(environ);routeUrl=routerRequest.getRouteUrl()# 获取路由解析后url地址,比如news/indexrouteParams=routerRequest.getRouteParams();# 获取路由的解析后参数{"id":1}解析命令行请求地址# 控制台输入: python3 main.py news/detail?id=2fromhrouter.routeimportRouterManagerconf={'customRouter':{'clazz':'easy','rules':[],'actionRule':{'filter':['site','controllers','modules'],'suffix':['Action','Controller'],'func':''#action 地址处理方法}},'routerRequest':"ConsoleRouterRequest",}routerManager=RouterManager(**conf);# 解析请求environ={};# uwsgi environ 上下文routerRequest=routerManager.runRoute(environ);routeUrl=routerRequest.getRouteUrl()# 获取路由解析后url地址,比如news/detailrouteParams=routerRequest.getRouteParams();# 获取路由的解析后参数{"id":2}规则路由fromhrouter.routeimportRouterManagerconf={'customRouter':{'clazz':'easy','rules':[# uri 请求地址规则,action 操作地址规则,method 请求方法,clazz 规则类,用于扩展#{'uri':'<news:\w+>/<id:\d+>','action':'<news>/index','method'='get','clazz'=>''},#{'uri':'<controller:\w+>/<action:\w+>','action':'<controller>/<action>'},{'uri':'<news:\w+>/<id:\d+>.html','action':'<news>/detail'}],'actionRule':{'filter':['site','controllers','modules'],'suffix':['Action','Controller'],'func':''#action 地址处理方法}}}routerManager=RouterManager(**conf);url=routerManager.buildUrl('news/detail',{"id":"10"})# url:news/detail?id=10生成地址fromhrouter.routeimportRouterManagerconf={'customRouter':{'clazz':'easy','rules':[# uri 请求地址规则,action 操作地址规则,method 请求方法,clazz 规则类,用于扩展#{'uri':'<news:\w+>/<id:\d+>','action':'<news>/index','method'='get','clazz'=>''},#{'uri':'<controller:\w+>/<action:\w+>','action':'<controller>/<action>'},{'uri':'<news:\w+>/<id:\d+>.html','action':'<news>/detail'}],'actionRule':{'filter':['site','controllers','modules'],'suffix':['Action','Controller'],'func':''#action 地址处理方法}}}routerManager=RouterManager(**conf);url=routerManager.buildUrl('news/detail',{"id":"10"})# url:news/detail?id=10装饰器注册路由规则fromhrouter.routeimportRouterManager,reg_route_ruleconf={'customRouter':{'clazz':'easy','rules':[# uri 请求地址规则,action 操作地址规则,method 请求方法,clazz 规则类,用于扩展#{'uri':'<news:\w+>/<id:\d+>','action':'<news>/index','method'='get','clazz'=>''},#{'uri':'<controller:\w+>/<action:\w+>','action':'<controller>/<action>'},{'uri':'<news:\w+>/<id:\d+>.html','action':'<news>/detail'}],'actionRule':{'filter':['site','controllers','modules'],'suffix':['Action','Controller'],'func':''#action 地址处理方法}}}routerManager=RouterManager(**conf);# 注册路由规则-函数@reg_route_rule('getuser')defgetuser(self):return"<h1>您好</h1>"# 注册路由规则-类方法classNewsController:@reg_route_rule('news/list',method='post')defindex(self):return"<h1>您好</h1>"# 资讯详情defdetail(self):return"<h1>您好</h1>"# 创建路由request 对象routerRequest=routerManager.runRoute({'PATH_INFO':"news/list"});# 获取解析后的路由地址routeUrl=routerRequest.getRouteUrl()# 获取路由解析后url地址,比如news/indexrouteParams=routerRequest.getRouteParams();# 获取路由的解析后参数{"id":1}# route = news/getindex# 生成地址url=routerManager.buildUrl('news/getuser',{"id":"10"})# url: news/getuser?id=10url=routerManager.buildUrl('getuser',{"id":"10"})# url: getuser?id=10url=routerManager.buildUrl('news/detail',{"id":"10"})# url: news/10.htmlurl=routerManager.buildUrl('account/user/add',{"id":"10"})# url: account/user/add?id=10