【问题标题】:Extending properly in CodeIgniter在 CodeIgniter 中正确扩展
【发布时间】: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


    【解决方案1】:

    如果您要从其他控制器访问$data 数组,则必须将其设置为属性

    class MY_Controller extends CI_Controller
    {
        public $layout;
        public $id;
        public $data = array();
    
        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->data = $data;
    
            $this->id = $this->session->userdata('user_id');
            $this->layout = 'layout/dashboard';
        }
    }
    

    如果您使用全 php 代码,还有一个建议remove your php end tag

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 2012-07-22
      • 2017-03-11
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多