Skip to main content

hehey-hrouter 是一个python 路由工具组件,仿Yii2 路由规则

Project description

hehey-hrouter

介绍

hehey-hrouter 是一个python 路由工具组件,仿Yii2 路由规则

基本示例

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':'<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'}
        ],

        # 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 "<h1>您好</h1>"

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

    @reg_route_rule('news/list')
    def getindex(self):

        print("pass")

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

    # 资讯详情
    def detail(self):
        print("pass")

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

    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)


  

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hehey-hrouter-1.0.0.tar.gz (8.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page