suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Wie übergibt Codeigniter Werte von my_controller an andere Controller?

My_Controller sagt Folgendes:


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;
    }
}

Dann bin ich im 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();

//***

Das ist derzeit machbar, aber es muss in der Seitenmethode geschrieben werden$data=$this->get_right();这句。
感觉不方便。想在__construct后就能获取到$data
Wie soll es geschrieben werden?

仅有的幸福仅有的幸福2829 Tage vor375

Antworte allen(1)Ich werde antworten

  • 为情所困

    为情所困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();
    
    //***

    Antwort
    0
  • StornierenAntwort