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