CodeIgniter: global variables in a controller

前端 未结 5 1231
一整个雨季
一整个雨季 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:41

    You can setup a class property named data and then set it's default values into the contructor which is the first thing which is run when a new instance on Basic is created. Then you can reference to it with the $this keyword

    class Basic extends Controller
    {
       var $data = array();
    
       public function __construct()
       {
           parent::__construct();
           // load config file if not autoloaded
           $this->data['title'] = 'Page Title';
           $this->data['robots'] = 'noindex,nofollow';
           $this->data['css'] = $this->config->item('css');
       }
    
       function index()
       {
           $this->data['my_data'] = 'Some chunk of text';
           $this->load->view('basic_view', $this->data);
       }
    
       function form()
       {
           $this->data['my_data'] = 'Another chunk of text';
           $this->load->view('form_view', $this->data);
       }
    }
    

提交回复
热议问题