【发布时间】:2012-09-30 10:40:51
【问题描述】:
我有几个 $data 在控制器中的几乎所有函数中都被调用。有没有办法在 __construct 函数中创建这个 $data 并将它们与被调用函数中的 $data 结合起来?示例:
function __construct() {
parent::__construct();
$this->load->model('ad_model', 'mgl');
$this->load->model('global_info_model', 'gi');
$this->load->model('user_model', 'um');
$this->load->library('global_functions');
$this->css = "<link rel=\"stylesheet\" href=\" " . CSS . "mali_oglasi.css\">";
$this->gi_cat = $this->gi->gi_get_category();
$this->gi_loc = $this->gi->gi_get_location();
$this->gi_type = $this->gi->gi_get_type();
}
function index() {
$count = $this->db->count_all('ad');
$data['pagination_links'] = $this->global_functions->global_pagination('mali_oglasi', $count, 2);
$data['title'] = "Mali Oglasi | 010";
$data['oglasi'] = $this->mgl->mgl_get_all_home(10);
$data['loc'] = $this->gi_loc;
$data['cat'] = $this->gi_cat;
$data['stylesheet'] = $this->css;
$data['main_content'] = 'mali_oglasi';
$this->load->view('template',$data);
}
如果我想放 $data['loc'], $data['cat'] 和 $data['stylesheet'] 在 __construct 我将不得不在 $this->load->view('template',$data);
有没有办法把这两者结合起来?
【问题讨论】:
标签: php codeigniter codeigniter-2