將類屬性$data
添加到Admin_controller類定義中。 $ data屬性將可用於擴展Admin_controller的每個控制器。因爲它是一個類屬性,所以它的訪問語法爲$this->data
。
class Admin_controller extends CI_Controller
{
//our new class property
protected $data = array();
public function __construct(){
parent :: __construct();
// do what is needed to get $ion_auth working
}
}
在延伸Admin_controller
任何類在構造函數中設置$data['$user_email']
。它隨後可被給出$這個 - >數據
class Some_controller extends Admin_controller
{
public function __construct(){
parent :: __construct();
//I am assuming that by this time $this->ion_auth->user() exists
//so we add a key and value to the class' $data property
//Note the use of the "$this->" syntax)
$this->data['user_email'] = $this->ion_auth->user()->row();
}
public function sets_up_a_view(){
//do stuff until you're ready for the header
//note that we are sending the class property "$this->data" to the view
$this-load->view('header', $this->data);
//load other views as needed using $this-data or other array - your choice
}
public function some_other_view(){
//send class property to view
$this-load->view('header', $this->data);
$data['foo'] = 42;
//send local var to view
$this-load->view('other_parts', $data);
}
}
注意兩個sets_up_a_view()
和some_other_view()
發送類屬性「$這個 - >數據」來header.php
每個視圖。但在some_other_view()
我們設置了一個名爲$data
的本地變量,發送到other_parts.php
視圖。
'header.php'是一個視圖嗎? – DFriend
@D朋友是的。 – mfgabriel92