CodeIgniter: global variables in a controller

前端 未结 5 1209
一整个雨季
一整个雨季 2020-12-24 13:56

I\'m a very newbie on CodeIgniter, and while I go on I run into problems that, in the procedural coding, were easy to fix

The current issue is: I have this controlle

5条回答
  •  有刺的猬
    2020-12-24 14:44

    Even though its been so long. It can be helpful to other you can use $this->load->vars($data); in core MY_controller to make $data array available in all views.

    /* Location: ./application/core/MY_Controller  */
    
    class MY_Controller extends CI_Controller {
    
    function __construct()
    {
        parent::__construct();
        $data['title'] = 'Page Title';
        $data['robots'] = 'noindex,nofollow';
        $data['css'] = $this->config->item('css');
        $this->load->vars($data);
    }
    
    }
    
     /* Location: ./application/controllers/start.php */
    class Start extends MY_Controller {
    
    function __construct()
    {       
        parent::__construct();
    }
    
    public function index()
    {
        $data['myvar'] = "mystring";
    
        $this->load->view('header');
        $this->load->view('start', $data);
        $this->load->view('footer');
    }
     }
    

提交回复
热议问题