如何将片段加载到函数中以减少重复代码。
例如,如果我使用以下代码;
public function site()
{
$this->load->model('model_get');
$data["results"] = $this->model_get->getData(__FUNCTION__);
$this->load->view('view_head', $data);
$this->load->view('view_nav');
$this->load->view('view_content_' . __FUNCTION__, $data);
$this->load->view('view_footer');
}
我怎么能把它减少到像这样的光:
public function site()
{
command $this->load->model('site_loader');
//Function specific code
}
使用 site_loader.php 包含:
$this->load->model('model_get');
$data["results"] = $this->model_get->getData(__FUNCTION__);
$this->load->view('view_head', $data);
$this->load->view('view_nav');
$this->load->view('view_content_' . __FUNCTION__, $data);
$this->load->view('view_footer');
基于 totymedli 建议的工作代码。
Controller
public function site()
{
$pagename = __FUNCTION__;
include 'resources/scripts/site_loader.php';
}
Site Loader
<?php
$this->load->model('model_get');
$data["results"] = $this->model_get->getData($pagename);
$this->load->view('view_head', $data);
$this->load->view('view_nav');
$this->load->view('view_content_' . $pagename, $data);
$this->load->view('view_footer');
?>