我有一个小的codeigniter控制器.下面是代码
class Example extends CI_Controller {
/*
* Constructor function
*/
function __construct() {
parent::__construct();
$data['extraScripts'] = 'test'; //Use to add extra scripts in head
}
function function1() {
$this->load->view('v1',$data);
}
function function2() {
$data['extraScripts'] = 'extraScript Veriable override here';
$this->load->view('v2',$data);
}
Run Code Online (Sandbox Code Playgroud)
我想要的是$data['extraScripts'] 在Controller的构造函数中定义一个可验证的,并且默认情况下希望在该控制器的每个方法中都是可验证的.我的意思是在函数f1中我没有创建extraScripts变量但它的视图应该从构造函数(或从任何其他方法)获取值,并且不应该给我undefine变量错误.在第二个函数f2中,我覆盖了extraScript变量,因此它的视图应该显示覆盖的文本.那可能吗.
使$ data成为一个属性(基本OOP).
例如;
class Example extends CI_Controller {
/*
* Constructor function
*/
public $data = array();
function __construct() {
parent::__construct();
$this->data['extraScripts'] = 'test'; //Use to add extra scripts in head
}
function function1() {
$this->load->view('v1',$this->data);
}
function function2() {
$data['extraScripts'] = 'extraScript Veriable override here';
$this->load->view('v2',$this->data);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4050 次 |
| 最近记录: |