1,定义路由
URL地址里面的index模块怎么才能省略呢,默认的URL地址显得有点长,下面就来说说如何通过路由简化URL访问。
我们在路由定义文件(application/route.php)里面添加一些路由规则
return [
// 添加路由规则 路由到 index控制器的hello操作方法
‘hello/:name‘ => ‘index/index/hello‘,
];
2,设置URL分隔符
如果需要改变URL地址中的pathinfo参数分隔符,只需要在应用配置文件(application/config.php)中设置:
// 设置pathinfo分隔符
‘pathinfo_depr‘ => ‘-‘,
3,例子
return [
‘blog/:year/:month‘ => [‘blog/archive‘, [‘method‘ => ‘get‘], [‘year‘ => ‘\d{4}‘, ‘month‘ => ‘\d{2}‘]],
‘blog/:id‘ => [‘blog/get‘, [‘method‘ => ‘get‘], [‘id‘ => ‘\d+‘]],
‘blog/:name‘ => [‘blog/read‘, [‘method‘ => ‘get‘], [‘name‘ => ‘\w+‘]],
];