【发布时间】:2012-05-14 17:26:40
【问题描述】:
我是 CodeIgniter 的新手,在继续进行过程中,我遇到了一些在程序编码中很容易解决的问题
当前的问题是:我有这个控制器
class Basic extends Controller {
function index(){
$data['title'] = 'Page Title';
$data['robots'] = 'noindex,nofollow';
$data['css'] = $this->config->item('css');
$data['my_data'] = 'Some chunk of text';
$this->load->view('basic_view', $data);
}
function form(){
$data['title'] = 'Page Title';
$data['robots'] = 'noindex,nofollow';
$data['css'] = $this->config->item('css');
$data['my_other_data'] = 'Another chunk of text';
$this->load->view('form_view', $data);
}
}
如您所见,一些数组项反复重复:
$data['title'] = 'Page Title';
$data['robots'] = 'noindex,nofollow';
$data['css'] = $this->config->item('css');
难道没有办法让它们在控制器中“全局化”,这样我就不必为每个函数键入它们了吗? 类似的东西(但这给了我错误):
class Basic extends Controller {
// "global" items in the $data array
$data['title'] = 'Page Title';
$data['robots'] = 'noindex,nofollow';
$data['css'] = $this->config->item('css');
function index(){
$data['my_data'] = 'Some chunk of text';
$this->load->view('basic_view', $data);
}
function form(){
$data['my_other_data'] = 'Another chunk of text';
$this->load->view('form_view', $data);
}
}
提前致谢!
【问题讨论】:
标签: arrays codeigniter controller global-variables