简体   繁体   中英

CodeIgniter - every controller need 3 variables passed to views

I'm in the middle of cleaning up a web app written in CodeIgniter. Each main view has a sidebar that displays some common database driven data - recent updates, news, etc. I have the queries cached to help speed things up a bit but I'm also looking for a better way to make this happen.

Right now each controller loads the model, loads up the $data array and passes it to the view.

I've extended the main CI controller class and am not loading up the data in the constructor which has simplified the code a bit but I'm still left feeling like there should be a better way. Any suggestions?

not sure if that's what you're asking , but you can have a extra parent/base controller to load repetitive data like news , updates , etc...

class baseController extends CI_Controller {


    public function __construct(){
        parent::__construct();
    }

    function viwe($view = 'defualt' , $data = array()){

      $data['updates'] = $this->db->get('updates');
      $data['news']    = $this->db->get('news');
      $this->load->view($view , $data );
    }

}

another controller :

include "baseController.php";
class another extends baseController {

    public function __construct(){
        parent::__construct();
    }

    function index()
    { 
        $data['thisData']  = $this->db->get('thisdata'); 
        $this->view('thisView' , $data  );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM