Data available for all views in codeigniter

后端 未结 8 1238
日久生厌
日久生厌 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:08

    You need to extend CI_Controller to create a Base Controller:

    https://www.codeigniter.com/user_guide/general/core_classes.html

    core/MY_Controller.php

    'some_data');
    
             //Send the data into the current view
             //http://ellislab.com/codeigniter/user-guide/libraries/loader.html
             $this->load->vars($global_data);
    
         }  
    }
    

    controllers/welcome.php

     class Welcome extend MY_Controller {
          public function index() {
              $this->load->view('welcome');
          }
     }
    

    views/welcome.php

    var_dump($some_var);

    Note: to get this vars in your functions or controllers, you can use $this->load->get_var('some_var')

提交回复
热议问题