后端绑定域名的方法就不写了很简单的curd
前端app\index\controller\ba
<?php
namespace app\index\controller;
use think\Controller;
use app\common\model\Amoy as AmoyModel;
/**
* Base
*/
class Base extends Controller
{
public $site_data = [];//当前店铺数据
public function _initialize()
{
if (input('__domain__') == null) {
//启用顶级域名处理
$domain = $this->request->domain();//获取当前的域名
//判断当前域名是否为系统根域名
if ((get_domain($domain) == config('url_domain_root')) == false) {
//当前域名为自有域名
//将域名进行处理
$domain = str_replace("http://","",$domain);//去除http://
//在数据库中查找当前域名
$data = AmoyModel::where(['status'=>1,'site_level_domain_status'=>1,'site_level_domain'=>$domain])->find();
//如果为空则返回错误信息
if (empty($data)) {
return $this->error('顶级域名所属的店铺不存在,或者状态异常');
}
}else{
//默认访问模式链接需要传site_id参数
$site_id = input('site_id/d');
$data = AmoyModel::where(['id'=>$site_id,'status'=>1])->find();
if (empty($data)) {
return $this->error('店铺不存在,或者状态异常');
}
}
//将店铺数据赋值给全局变量
$this->site_data = $data;
}else{
$this->_domain();//启用域名分发
}
//判断是否为移动端环境自动跳转移动端分组
if ($this->request->isMobile()==true) {
if (input('site_id/d')==0) {
return $this->redirect("mobile.index/index");
}else{
return $this->redirect("mobile.index/index",['site_id'=>input('site_id/d')]);
}
}
}
/**
* 二级域名分发处理
* @return [type] [description]
*/
protected function _domain(){
//获取二级域名
$site_domain = input('__domain__/s');
//汉字正则
$a=preg_match('/['.chr(0xa1).'-'.chr(0xff).']/', $site_domain);//是否有汉字
//数字正则
$b=preg_match('/[0-9]/', $site_domain);
//拼音正则
$c=preg_match('/[a-zA-Z]/', $site_domain);
//判断域名是否符合正则要求
if ($site_domain == '' || strlen($site_domain) < 3 || strlen($site_domain) > 12 || $a) {
//返回错误
return $this->error('域名信息有误');
}
//进数据库查询二级域名所属的信息
$data = AmoyModel::where(['site_domain'=>$site_domain,'status'=>1])->find();
if (empty($data)) {
//数据不存在返回错误
return $this->error('子域名所属的店铺不存在,或者状态异常');
}
//店铺数据赋值给全局变量
$this->site_data = $data;
}
}
?>
common.php /**
* 获取连接中的主域名
* @param [type] $url [description]
* @return [type] 这个函数是根据链接获取连接中的顶级域名 返回值为string
*/
function get_domain($url){
$pattern = "/[w-]+.(com|net|org|gov|cc|biz|info|cn)(.(cn|hk))*/";
preg_match($pattern, $url, $matches);
if(count($matches) > 0) {
return $matches[0];
}else{
$rs = parse_url($url);
$main_url = $rs["host"];
if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
return $main_url;
}else{
$arr = explode(".",$main_url);
$count=count($arr);
$endArr = array("com","net","org","3322");//com.cn net.cn 等情况
if (in_array($arr[$count-2],$endArr)){
$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
}else{
$domain = $arr[$count-2].".".$arr[$count-1];
}
return $domain;
}// end if(!strcmp...)
}// end if(count...)
}// end
配置文件config.php 文件里面必须启用 // 域名部署
'url_domain_deploy' => true,
// 域名根,如thinkphp.cn
'url_domain_root' => 'xjyili.cn',
//end
route.php路由文件配置参数:return [
'__pattern__' => [
'name' => '\w+',
],
'__domain__' => [
'admin' => 'user',//这个和没多大关系是绑定admin二级域名到user模块
'*' => 'index?site_domain=*'//这个是非常重要的,site_domain这个参数是传递二级域名信息
],
'[hello]' => [
':id' => ['index/hello', ['method' => 'get'], ['id' => '\d+']],
':name' => ['index/hello', ['method' => 'post']],
],
];
还有什么不懂的欢迎下方留言或者QQ:644332569交流,本人博客:新疆网站建设