仿58一站多城市的实现及路由配置(一)

jerry thinkphp 2015年11月19日 收藏
最近在做一个电商项目,需要实现一站多城市,看了不少帖子,发现都有些瑕疵,最后想到了使用空控制器访问城市分站。
现在把代码贴出来分享给大家,同时征求更好的优化建议。
例如zz.58.com这种就不做介绍了,如谁用到,可以私信我。
一站多城市同时,一站还多个后台,以58为例。
Common/Conf/config.php
  1. return array(
  2.     'APP_SUB_DOMAIN_DEPLOY'   =>    1, // 开启子域名配置
  3.     'APP_SUB_DOMAIN_RULES'    =>    array(
  4.         'www'        => 'Home',//使用www.58.com访问
  5.         'biz'        => 'Admin',//使用biz.58.com访问
  6.         'm'          => 'Wap',//使用m.58.com访问
  7.     ),
  8. );
Home/Conf/config.php
  1. return array(
  2.     /* 开启路由功能 */
  3.     'URL_ROUTER_ON'   => true,
  4.     'URL_ROUTE_RULES'=>array(
  5.     //静态路由优先
  6.         'cities'      => 'City/Change',//使用www.58.com/cities访问,用于选择城市
  7.         'login' => 'Public/login',//使用www.58.com/login访问,用于登陆
  8.         'reg'   => 'Public/reg',//使用www.58.com/reg访问,用于注册
  9.     //动态路由开始
  10.         'buy/:id\d' => 'Order/buy',//使用www.58.com/buy/18访问,用于购买ID为18的商品
  11.                 ':city/deal/:id\d' => 'Deal/index',//使用www.58.com/zz/deal/18访问,用于打开郑州页面的ID为18的商品页面
  12.         ':city/:category'  => 'Category/list',//使用www.58.com/zz/meishi访问,用于打开郑州美食分类
  13.         ),
  14. );
Home/Controller/CommonController.class.php
  1. namespace Home\Controller;
  2. use Think\Controller;
  3. class CommonController extends Controller {
  4.         /**
  5.      * 后台控制器初始化
  6.      */
  7.     protected function _initialize(){
  8.     header('Content-type:text/html;charset=utf-8');
  9.         /* 读取数据库中的配置 */
  10.         $config =   S('DB_CONFIG_DATA');
  11.         if(!$config){
  12.             $config =  D('Admin/Config')->getConfig();
  13.             S('DB_CONFIG_DATA',$config);
  14.         }
  15.         C($config); //添加配置
  16.     if(C('WEB_SITE_STATUS') == 'N'){
  17.         $this->assign("message",'网站维护中,暂时无法访问');
  18.         $this->error();
  19.         }
  20.         /* 获取UID */
  21.     if (session('?uid') == false) {
  22.             session('uid','0');
  23.         }
  24.     }
  25. }
Home/Controller/IndexController.class.php
  1. namespace Home\Controller;
  2. use Think\Controller;
  3. class IndexController extends CommonController {
  4.     
  5.     public function _before_index(){
  6.         /* 判断cookie */
  7.         if (isset($_COOKIE['cityid'])){
  8.             $city = $_COOKIE['cityurl'];
  9.             redirect('/'.$city);
  10.         }else{
  11.             redirect('cities');
  12.         }
  13.     }
  14.     
  15.     public function index(){
  16.         echo '如果看到这个页面,说明程序出错了!';
  17.     }
  18.     
  19. }
Home/Controller/EmptyController.class.php
  1. namespace Home\Controller;
  2. use Think\Controller;
  3. class EmptyController extends CommonController{
  4.     public function index(){
  5.         //根据当前控制器名来判断要执行那个城市的操作
  6.         $cityName = strtolower(CONTROLLER_NAME);
  7.         if (isset($_COOKIE['cityurl']) and $cityName == $_COOKIE['cityurl']){
  8.             $this->city($cityName);
  9.         }else{
  10.             $cityinfo = M('CategoryArea')->field('id,title,url')->where(array('url'=>$cityName,'type'=>'0','status'=>'1'))->find();
  11.         if ($cityinfo){
  12.             cookie('cityid',$cityinfo['id']);
  13.             cookie('cityurl',$cityinfo['url']);
  14.             cookie('citytitle',$cityinfo['title']);
  15.             $this->city($cityName);
  16.         }else{
  17.             cookie('cityid',null);
  18.             cookie('cityurl',null);
  19.             cookie('citytitle',null);
  20.             redirect('cities');
  21.         }
  22.         }
  23.     }
  24.     //注意 city方法 本身是 protected 方法
  25.     protected function city($name){
  26.         pre (session());
  27.         pre (cookie());
  28.         if(isset($_GET['p'])){$p = $_GET['p'];}else{$p=1;}
  29.         $this->assign('p',$p);//当前页数
  30.         $this->assign('category',D('Category')->categoryList(''));//获取分类及数量
  31.         //防止程序提示错误,设定为0
  32.         $info['categoryid'] = 0;$info['category_2'] = 0;
  33.         $this->assign('area',D('CategoryArea')->areaList($info));//获取城市区域分类
  34.         $data['city'] = $name;
  35.         if (isset($_GET['order'])){$data['order'] = $_GET['order'];}else{$data['order'] = '';}
  36.         pre ($data);
  37.         $this->assign('data',$data);//获取列表
  38.         $this->assign('team',D('Team')->teamList_i($data,$p));//获取列表
  39.         $this->assign('page',D('Team')->pageshow_i($data,$p));//获取列表
  40.         $this->assign('pagetype',ACTION_NAME);
  41.         $this->display('Index/index');
  42.     }
  43. }