如何在目录" application / core"中的MY_Loader.php -file中加载模型?
我试图将我的loader-views分组到MY_Loader文件中,以便更方便地加载视图。我想将动态值传递给标题以自定义标题输出。
我的代码如下所示:
private $ci;
public function __construct()
{
parent::__construct();
// Initiate instance
$this->ci =& get_instance();
// Load model
$this->ci->load->model('My_loader_model');
}
public function template($template_name, $vars = array(), $return = FALSE)
{
$data_header['inbox_messages'] = $this->ci->My_loader_model->inbox_messages_count();
$content = $this->view('header', $data_header, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('footer', $vars, $return);
if ($return)
{
return $content;
}
}
}
答案 0 :(得分:0)
使用核心文件夹中的CI_Controler的commondata扩展创建1个文件。 将Common模型设置为自动加载
function getSettingData($id)
{
return $this->common->selectbyid($id);
}
}
将此文件包含在myLoader中
/ ** * /application/core/MY_Loader.php * * / MY_Loader类扩展了CI_Loader {
function __construct() {
parent::__construct();
}
public function template($template_name, $vars = array(), $return = FALSE) {
$requestUri = $_SERVER['REQUEST_URI'];
include(dirname(__FILE__)."/commondata.php");
$settingdata = new commondata();
$vars['companyname'] = $settingdata->getSettingData(1);
$content = "";
$content = $this->view('templates/header_admin', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer_admin', $vars, $return);
return $content;
}
}