我没有找到类似的话题,所以我问:
如何克服对所有CodeIgniter控制器函数重复相同模型查询的问题?对于我的站点,我必须在几乎相同的重复数据库查询上构建页眉和页脚,如下所示:
class Main extends CI_Controller
{
public function aboutus()
{
$this->load->model("read_db");
$commondata["title"] = "Company - ".lang("aboutus");
$commondata["mainmenu"] = $this->read_db->db_mainmenu();
$commondata["mainprodcat"] = $this->read_db->db_allprodmaincat();
$commondata["bestselling"] = $this->read_db->db_bestselling();
$commondata["brochures"] = $this->read_db->db_allbrochures();
$this->load->view("headerview", $commondata);
$contentdata["aboutus"] = $this->read_db->db_aboutus();
$this->load->view("view_aboutus", $contentdata);
$this->load->view("footerview");
}
public function contact()
{
$this->load->model("read_db");
$commondata["title"] = "Company - ".lang("contact");
$commondata["mainmenu"] = $this->read_db->db_mainmenu();
$commondata["mainprodcat"] = $this->read_db->db_allprodmaincat();
$commondata["bestselling"] = $this->read_db->db_bestselling();
$commondata["brochures"] = $this->read_db->db_allbrochures();
$this->load->view("headerview", $commondata);
$contentdata["aboutus"] = $this->read_db->db_contact();
$this->load->view("contactview", $contentdata);
$this->load->view("footerview");
}
*further functions like this*
}是否有将相同的重复模型调用外包给另一个函数或文件的选项?非常感谢你的建议。
发布于 2013-06-02 08:25:33
您可以在模型中创建一个组函数,然后仅从控制器调用该组函数。例如:
模型
function db_bundle() {
$data = array();
$data["mainmenu"] = $this->db_mainmenu();
$data["mainprodcat"] = $this->db_allprodmaincat();
$data["bestselling"] = $this->db_bestselling();
$data["brochures"] = $this->db_allbrochures();
return $data;
}控制器
$commondata = $this->read_db->db_bundle();注意,代替捆绑,你可以根据需要的页面对你的呼叫进行分组,这样你就可以有单独的组来联系我们,关于我们等等。或者,您可以让捆绑包函数接受参数,这些参数将允许您控制应该和不应该从内部调用哪些函数。
https://stackoverflow.com/questions/16878519
复制相似问题