程序员

图解ThinKPHP5之URL与路由入门二

2019-02-24  本文已影响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

上一篇 下一篇

猜你喜欢

热点阅读