1、在控制器页面:
<?php // 本类由系统自动生成,仅供测试用途 namespace Home\Controller; use Think\Controller\RestController; class IndexController extends RestController{ //显示页面 public function index(){ //print_r($this->_method);die; $Name = $_GET['data']; if(!preg_match("/^[A-Za-z]+$/",$Name)){ echo "输入有误";die; } if(__EXT__ == ''){ $type="html"; }else{ $type=__EXT__; } switch ($this->_method){ case 'get': // get请求处理代码 if (__EXT__ == 'html'){ $html = $this->sel($Name); $this->response($html,'html'); //echo "html"; }elseif(__EXT__ == 'xml'){ $xml = $this->sel($Name); $this->response($xml,'xml'); }elseif(__EXT__ == 'json'){ $json = $this->sel($Name); $this->response($json,'json'); } break; case 'put': // put请求处理代码 break; case 'post': // post请求处理代码 break; } } public function sel($name){ //print_r($name);die; $sheng = M('sheng'); //实例化表 $city = M('city'); $xian = M('xian'); if($name=="china"){ $data=$sheng->select(); return $data; }else{ $s_id =$sheng->getfieldbypyname($name,'id'); //获取查询省id //print_r($s_id);die; //判断获取省id是否为空,如果不为空查询获取省的下级的关联id的相关数据 if(!$s_id==""){ $c_id['p_id']=$s_id; $datas = $city->where($c_id)->select(); //print_r($datas);die; return $datas; }else{ $city_id =$city->getfieldbypyname($name,'id'); //获取市id getFieldBy 根据字段查询并返回某个字段的值 例如,getFieldByName //print_r($city_id);die; if(!$city_id==""){ $x_id['p_id']=$city_id; $data1 = $xian->where($x_id)->select(); //print_r($data1); return $data1; }else{ $xian_id = $city_id =$xian->getfieldbypyname($name,'id'); //print_r($xian_id);die; if(!$xian_id==""){ $cx_id['id']=$xian_id; //print_r($cx_id);die; $data2 = $xian->where($cx_id)->select(); //print_r($data2);die; return $data2; }else{ echo "没有该地区!"; } } } } } }
2、配置文件:
<?php return array( //'配置项'=>'配置值' /* 数据库设置 */ 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => '127.0.0.1', // 服务器地址 'DB_NAME' => 'news', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => '', // 密码 'DB_PORT' => '3306', // 端口 'URL_ROUTER_ON' => true, //开启路由 'URL_ROUTE_RULES'=>array( 'Index/:data' => array('Index/index', 'status=1'), ), ); ?>
本文转自噼里啪啦啦 51CTO博客,原文链接:http://blog.51cto.com/pilipala/1651561如需转载请自行联系原作者