###在使用路由之前你需要先配置路由
kcw提供了两种路由模式,分别是混合路由和独立路由
混合路由意味着您可以使用自己配置的路由的同时还可以使用系统提供的默认路由(参考url访问)
```
#路由配置
route['default']=True #是否开启默认路由 默认路由开启后面不影响以下配置的路由,模块名/版本名/控制器文件名/方法名 作为路由地址 如:http://www.kcw.com/modular/plug/index/index/
route['modular']='' #指定访问配置固定模块 (如果配置了该值,将无法通过改变url访问不同模块)
route['plug']='' #指定访问固定插件 (如果配置了该值,将无法通过改变url访问不同插件)
route['defmodular']='index' #默认模块 当url不包括模块名时
route['defplug']='index' #默认插件 当url不包括插件名时
route['files']='index' #默认路由文件(控制器) 当url不包括控制器名时
route['funct']='index' #默认路由函数 (操作方法) 当url不包括操作方法名时
route['methods']=['POST','GET'] #默认请求方式
route['children']=[
{'title':'首页','path':'','component':'api/v1/index/home','methods':['POST','GET']},
]
```
###路由定义
如果我们配置了以下内容
```
route['children']=[
{'title':'首页','path':'','component':'api/v1/index/home','methods':['POST','GET']},
]
```
我们访问:
```
http://127.0.0.1:39001
```
会路由到
```
http://127.0.0.1:39001/api/v1/index/home
```
###路由参数
如果我们配置了以下内容
```
route['children']=[
{'title':'首页','path':'/home/:id/:title','component':'api/v1/index/home','methods':['POST','GET']},
]
```
我们访问:
```
http://127.0.0.1:39001/home/1/hello
```
会路由到
```
http://127.0.0.1:39001/api/v1/index/home/1/hello
```
###注意 (固定了模块和固定了插件)下面的配置将无法通过改变url访问不同的插件和模块
如果我们配置了以下内容
```
route['modular']='modular1'
route['plug']='plug1'
```
路由定义
```
route['children']=[
{'title':'首页','path':'','component':'index/home','methods':['POST','GET']},
]
```
我们访问:
```
http://127.0.0.1:39001
```
会路由到
```
http://127.0.0.1:39001/modular1/plug1/index/home
```
### modular和plug支持域名路由,该配置不影响默认路由
如果我们配置了以下内容
```
route['modular']=[{"kcwebapi":"modular1"}]
route['plug']=[{"kcwebapi":"plug1"}]
```
通过kcwebapi开头的二级域名访问
```
http://kcwebapi.kcw.cn/index/home
```
会路由到
```
http://127.0.0.1:39001/modular1/plug1/index/home
```
更多实例可以自行测试
###路由获取
通过以下方法
```
request.getroutecomponent()
```
会得到
```
(['POST', 'GET'], 'api', 'plug1', 'index', 'inter', ('123212',))
(['POST', 'GET'], '模块', '插件', '文件名', '方法名', ('参数1',))
```
###URL生成
会根据当前url自动生成是否拼接模块或者插件
```
request.url('/控制器名/方法名?参数=值')
```
kcws官方开发手册
kapi1.0
kapi1.0
kcws
2.6.5
2.6.4
2.6.3
2.6
2.5
2.3
develop
2.2
2.1
登录