dd

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

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