Skip to main content

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)

基本示例

  • 快速使用
from hrouter.route import RouterManager
conf =  {
    '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/index
routeParams = routerRequest.getRouteParams();# 获取路由的解析后参数{"id":1}
# 生成url 地址
url = routerManager.buildUrl('news/detail',{"id":"10"})  
# url: news/detail?id=10
  • 解析web请求地址
# 浏览器输入:http://xxx.cn/news/index?id=1

from hrouter.route import RouterManager
conf =  {
    '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/index
routeParams = routerRequest.getRouteParams();# 获取路由的解析后参数{"id":1}
  • 解析命令行请求地址
# 控制台输入: python3 main.py news/detail?id=2

from hrouter.route import RouterManager
conf =  {
    '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/detail
routeParams = routerRequest.getRouteParams();# 获取路由的解析后参数{"id":2}
  • 规则路由
from hrouter.route import RouterManager
conf =  {
    '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
  • 生成地址
from hrouter.route import RouterManager
conf =  {
    '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
  • 装饰器注册路由规则
from hrouter.route import RouterManager,reg_route_rule
conf =  {
    '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')
def getuser(self):

    return "<h1>您好</h1>"

# 注册路由规则-类方法
class NewsController:

    @reg_route_rule('news/list',method = 'post')
    def index(self):

        return "<h1>您好</h1>"

    # 资讯详情
    def detail(self):

        return "<h1>您好</h1>"

# 创建路由request 对象
routerRequest = routerManager.runRoute({'PATH_INFO':"news/list"});
# 获取解析后的路由地址
routeUrl = routerRequest.getRouteUrl() # 获取路由解析后url地址,比如news/index
routeParams = routerRequest.getRouteParams();# 获取路由的解析后参数{"id":1}
# route = news/getindex

# 生成地址
url = routerManager.buildUrl('news/getuser',{"id":"10"})
# url: news/getuser?id=10
url = routerManager.buildUrl('getuser',{"id":"10"})
# url: getuser?id=10
url = routerManager.buildUrl('news/detail',{"id":"10"})
# url: news/10.html

url = routerManager.buildUrl('account/user/add',{"id":"10"})
# url: account/user/add?id=10

Release files for hehey-hrouter 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hehey-hrouter 1.0.2
File Size Uploaded
hehey-hrouter-1.0.2.tar.gz 9.8 kB Details

Release files / hehey-hrouter-1.0.2.tar.gz

Download URL hehey-hrouter-1.0.2.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
dbc54ba3a7d0136e8b0526637e41348c89876d95cb4299058880fc44097eb5c6
BLAKE2b-256 checksum
How to use checksums
1e84fffc983ffe693c173e9153b428d3d860aeee88371929a3add29004d4a621
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/20.7.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8

Release history Release notifications | RSS feed

This release

1.0.2 This release

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page