search

Home  >  Q&A  >  body text

How does codeigniter pass values ​​from my_controller to other controllers?

My_Controller says this:


class BaseController extends My_Controller {
    // public $data;
    public function __construct() {
        parent::__construct();
        // $this->get_right();
    }
    public function get_right(){
        $data['rightinfos'] = $this->Com_model->get_top20info();
        $data['right0'] = $this->Com_model->get_site_new();
        $data['right1'] = $this->Com_model->get_site_hot();
        $data['right2'] = $this->Com_model->get_site_hot2();
        return $data;
    }
}

Then I am in the site controller:

class Site extends BaseController {
    public function __construct() {
        parent::__construct(); 
        $this->load->model('Site_model'); 
    }

    
    public function page() {
  
        $data=$this->get_right();
        print_r($data);
        die();

//***

This is currently feasible, but the sentence $data=$this->get_right(); must be written in the page method.
Feels inconvenient. If I want to get $data
after __construct, how should I write it?

仅有的幸福仅有的幸福2829 days ago370

reply all(1)I'll reply

  • 为情所困

    为情所困2017-05-16 16:45:33

    class BaseController extends My_Controller {
    
        public $data;
        
        public function __construct() {
            parent::__construct();
            $this->data = $this->get_right();
        }
        public function get_right(){
            $data['rightinfos'] = $this->Com_model->get_top20info();
            $data['right0'] = $this->Com_model->get_site_new();
            $data['right1'] = $this->Com_model->get_site_hot();
            $data['right2'] = $this->Com_model->get_site_hot2();
            return $data;
        }
    }
    class Site extends BaseController {
        public function __construct() {
            parent::__construct(); 
            $this->load->model('Site_model'); 
        }
    
        
        public function page() {
            print_r($this->data);
            die();
    
    //***

    reply
    0
  • Cancelreply