美文网首页程序员
图解ThinKPHP5之URL与路由入门二

图解ThinKPHP5之URL与路由入门二

作者: 周行知 | 来源:发表于2019-02-24 09:19 被阅读0次

URL与路由入门二

观察下面一段代码

public function hello($name = '张三', $sex='女')

    {           

        echo "hello: ".$name ." ".$sex;     

    }

如图所示:

下面运行上面一段代码,观察url与输出的结果,你能发现什么问题?

http://localhost:8989/php/mvc/TP5.0/public/admin/index/hello/name/周行知/sex/不男非女

答案:太麻烦了!

我们来看一下它是怎么配置的?

我们需要找到route.php这个配置文件D:\phpStudy\WWW\php\mvc\TP5.0\application\route.php

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

];

如图所示:

    '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

相关文章

网友评论

    本文标题:图解ThinKPHP5之URL与路由入门二

    本文链接:https://www.haomeiwen.com/subject/xsdvyqtx.html