URL与路由入门二
观察下面一段代码
public function hello($name = '张三', $sex='女')
{
echo "hello: ".$name ." ".$sex;
}
如图所示:
![](https://img.haomeiwen.com/i7520141/3785fb35ee22a99b.png)
下面运行上面一段代码,观察url与输出的结果,你能发现什么问题?
http://localhost:8989/php/mvc/TP5.0/public/admin/index/hello/name/周行知/sex/不男非女
![](https://img.haomeiwen.com/i7520141/93d15da143935f9e.png)
答案:太麻烦了!
我们来看一下它是怎么配置的?
我们需要找到route.php这个配置文件D:\phpStudy\WWW\php\mvc\TP5.0\application\route.php
![](https://img.haomeiwen.com/i7520141/a649160f9f2cd7d3.png)
打开route.php代码如下:
return [
'__pattern__' => [
'name' => '\w+',
],
'[hello]' => [
// ':id' => ['index/hello', ['method' => 'get'], ['id' => '\d+']],
// ':name' => ['index/hello', ['method' => 'post']],
],
'hello/[:name]' =>['index/index/hello',['method' => 'get', 'ext' => 'html']],
// 定义闭包
// 'hello/[:name]' => function ($name) {
// return 'Hello, 闭包' . $name . '!';
// },
'today/[:year]/[:month]' =>['index/index/today',['method'=>'get'],['year'=>'\d{4}','month'=>'\d{2}']],
];
如图所示:
![](https://img.haomeiwen.com/i7520141/1501ea0813b12584.png)
'hello/[:name]' =>['index/index/hello',['method' => 'get', 'ext' => 'html']],
method get请求方式
ext:代表后缀
http://localhost:8989/php/mvc/TP5.0/public/admin/index/hello/name.html
![](https://img.haomeiwen.com/i7520141/ce1a0a958b0206ae.png)
网友评论