Data available for all views in codeigniter

后端 未结 8 1231
日久生厌
日久生厌 2020-12-09 10:56

I have a variable, contaning data that should be present in the entire site. Instead of passing this data to each view of each controller, I was wondering if there is a way

相关标签:
8条回答
  • 2020-12-09 11:32

    Create a MY_Controller.php file and save it inside the application/core folder. In it, something like:

    class MY_Controller extends CI_Controller {
    
       public $site_data;
    
       function __construct() {
           parent::__construct();
           $this->site_data = array('key' => 'value');
       }
    }
    

    Throughout your controllers, views, $this->site_datais now available. Note that for this to work, all your other controllers need to extend MY_Controllerinstead of CI_Controller.

    0 讨论(0)
  • 2020-12-09 11:34

    you can use $this->load->vars('varname', $data);[ or load data at 1st view only] onse and use in any loaded views after this

    0 讨论(0)
提交回复
热议问题