编写路由:
‘URL_ROUTER_ON‘ => true, //开启路由 ‘URL_ROUTE_RULES‘=>array( ‘index/:weather‘=>‘Index/index‘, ),
控制器内:
<?php namespace Home\Controller; use Think\Controller\RestController; class IndexController extends RestController{ Public function Index(){ $name=$_GET[‘weather‘]; if(!preg_match("/^[a-zA-Z]+$/",$name)){ //判断是否为纯字母 echo 1308; die; } /* __EXT__ 获得后缀名 */ if(__EXT__==""){ //判断对方需要数据格式 $type="html"; }else{ $type=__EXT__; } switch ($this->_method){ //判断请求方式 case ‘get‘: $data=$this->sel($name); $this->response($data,$type); //转换格式并输出 break; default:break; } } public function sel($dizhi){ $sheng=M("sheng"); $city=M("city"); $xian=M("xian"); if($dizhi==‘china‘){ $res=$sheng->select(); return $res; }else{ $sid=$sheng->getFieldByPyname($dizhi,‘id‘); //相当于 select id from sheng where pyname = $dizhi if($sid==""){ $cid=$city->getFieldByPyname($dizhi,‘id‘); if($cid==""){ $xid=$xian->getFieldByPyname($dizhi,‘id‘); if($xid==""){ echo "县级数据请查上一级"; }else{ $condition[‘id‘] = $xid; $data=$xian->where($condition)->select(); return $data; } }else{ $condition[‘p_id‘] = $cid; $data=$xian->where($condition)->select(); return $data; } }else{ $condition[‘p_id‘] = $sid; $data=$city->where($condition)->select(); return $data; } } } }
原文地址:http://blog.csdn.net/demon0313/article/details/45741351