依崇稀

导航

 
<?php
namespace app\index\controller;

class City 
{
    public function _empty($name)
    {
        //把所有城市的操作解析到city方法
        return $this->showCity($name);
    }
    
    //注意 showCity方法 本身是 protected 方法
    protected function showCity($name)
    {
        //和$name这个城市相关的处理
         return '当前城市' . $name;
    }
}

接下来,我们就可以在浏览器里面输入

http://serverName/index/city/beijing/
http://serverName/index/city/shanghai/
http://serverName/index/city/shenzhen/

由于City并没有定义beijing、shanghai或者shenzhen操作方法,因此系统会定位到空操作方法 _empty中去解析,_empty方法的参数就是当前URL里面的操作名,因此会看到依次输出的结果是:

当前城市:beijing
当前城市:shanghai
当前城市:shenzhen
posted on 2020-05-08 08:45  依崇稀  阅读(98)  评论(0编辑  收藏  举报