生成URL
生成URL的方法如下:
$param = [
'name' => 'test',
'id' => 1, // 可为空字符'',但不要为null
'more' => '' // 路由规则中没有的参数
];
$url = boa::router()->url('home.content.show', $param);
url()参数含义及用法参见:router类url方法
匹配路由
如果开启路由功能,将优先检索路由表,注意$param数组中的参数要和路由规则中定义的参数一致,才被匹配,多余的参数也会被附加在生成的URL上。假设匹配以下路由规则:
[
'url' => '/content/{name}/5-{id}/',
'act' => 'home.content.show'
]
将得到$url为:/content/test/5-1/?more=
未获匹配
如果关闭路由功能或路由未获匹配,将依据type(URL类型)生成URL:
- type=0:得到 /index.php?m=home&c=content&a=show&name=test&id=1&more=
- type=1:得到 /index.php/home/content/show/name-test/id-1/more-/
- type=2:得到 /home/content/show/name-test/id-1/more-/
额外参数
对于路由规则中额外的参数,将以QUERY_STRING的方式附加在匹配到的url后面,比如:
$param = [
'name' => 'test',
'id' => 1,
'more' => 'a',
'other' => 2
];
$url = boa::router()->url('home.content.show', $param);
匹配后得到 /content/test/5-1/?more=a&other=2,未获匹配时URL生成规则如前所述