【发布时间】:2014-05-14 23:08:50
【问题描述】:
我有一个模板,我需要在其中加载某些信息,并且只想加载一次,因此我必须创建一个名为 MY_Controller 的扩展控制器。 但是我坚持将 MY_Controller 中的 $data 数组扩展到其他控制器。
这里是 MY_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $layout;
public $id;
public function __construct()
{
parent::__construct();
$this->output->nocache();
$this->load->model('subject_model');
$this->load->model('user_model');
$this->load->model('survey_model');
// This is the info I need for every controller and method in my app
$data['total_subjects'] = $this->subject_model->countSubjects();
$data['check_if_already_posted_it_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('it_survey', $this->id);
$data['total_users'] = $this->user_model->countUsers();
$data['subjects'] = $this->subject_model->get_all_subjects();
$data['schools'] = $this->subject_model->get_all_schools();
$data['subject_name'] = $this->subject_model->getSubjectNameById($this->id);
$data['school_name'] = $this->subject_model->getSchoolNameById($this->id);
$this->id = $this->session->userdata('user_id');
$this->layout = 'layout/dashboard';
}
}
?>
这当然给了我一个错误。我应该怎么做才能让这个正常工作而不重复自己并在每个方法中加载相同的数据数组,因为到目前为止,这是我让它正常工作的唯一方法。
【问题讨论】:
标签: php codeigniter codeigniter-2