嗨,这是我的解决方案,我用它来开发单核多站点创建一个设置表,在该表中保存域、站点名称和模板信息等基本信息,稍后添加许多设置,在核心目录检查域下创建 MY_Controller是否存在于您的数据库中
class MY_Controller extends CI_Controller {
public $site_id = 0;
public $template = '';
function __construct(){
parenet::__construct()
$this->loadSetting();
}
function loadSetting(){
$host = $_SERVER['HTTP_HOST'];
$row = $this->db->where_like('domain',$host)->get('setting')->row();
$this->site_id = $row->id;
$this->template = $row->template;
}
}
设置完成后,您可以将 site_id 作为外键添加到其他表(如页面、新闻)中,以获取站点特定页面和新闻以及有关站点的其他信息。现在像这样使用 MY_Controller 扩展您的页面控制器或新闻控制器。
class Page_Controller extends MY_Controller {
function __construct(){
parent::__construct();
}
function index(){
$page = $this->db->get_where('pages',array('site_id',$this->site_id))->row();
}
}
如果您进一步希望在应用程序中的每个位置都可以使用 site_id,您可以创建一个库并自动加载该库,您可以将 loadSetting 方法放入该库中。喜欢
class Setting {
private $ci;
public $site_id;
public $template;
function __construct(){
$this->ci = get_instance();
$this->loadSetting();
}
function loadSetting(){
$host = $_SERVER['HTTP_HOST'];
$row = $this->ci->db->where_like('domain',$host)->get('setting')->row();
$this->site_id = $row->id;
$this->template = $row->template;
}
}
现在您可以在任何地方访问应用程序中的 site_id,就像模型其他库和控制器一样
$this->setting->site_id;
希望这能解决您的问题。