CodeIgniter将新值推入配置文件的数组

我可能在PHP中缺少一些知识,似乎无法使其正确工作。CodeIgniter将新值推入配置文件的数组

我有这个值的application/config/cfg.backend.php

$config['head_meta']   = array(

'stylesheets' => array(

'template.css'

),

'scripts' => array(

'plugins/jquery-2.0.3.min.js',

'plugins/bootstrap.min.js'

),

'end_scripts' => array(

'plugins/jquery-ui.js',

'plugins/jquery.dataTables.min.js',

'template.js'

)

);

有我加载所有必要的脚本和CSS文件,所以每当我需要扩展一些阵列,我就干脆用一个array_push()功能像我的确在我的application/controllers/backend/Categories.php

class Categories extends Backend_Controller{ 

function __construct(){

parent::__construct();

// Load dependencies

$head_meta = config_item('head_meta');

array_push($head_meta['end_scripts'], 'plugins/redactor.min.js', 'categories.js');

array_push($head_meta['stylesheets'], 'redactor.css');

var_dump($head_meta['end_scripts']);

}

// THE REST OF THE CLASS ...

}

所以通过做var_dump($head_meta['end_scripts']),我看到array_push()做他的工作,但并没有加载我的剧本,我不知道为什么,我'stucked这里。

array (size=5) 

0 => string 'plugins/jquery-ui.js' (length=20)

1 => string 'plugins/jquery.dataTables.min.js' (length=32)

2 => string 'template.js' (length=11)

3 => string 'plugins/redactor.min.js' (length=23)

4 => string 'categories.js' (length=13)

任何建议我做错了什么?

==== ====修订

我有位于applications/views/backend/templates/template.php主模板文件,其中在页面的底部,我做了foreach()加载end_scripts

<?php foreach($this->config->item('end_scripts', 'head_meta') as $end_scripts):?> 

<script src="<?php echo base_url();?>assets/js/<?php echo $end_scripts;?>" type="text/javascript"></script>

<?php endforeach;?>

并且将一个特定视图加载到主templates/template.php中,我这样做:

// Insert catched data into the component view of main template 

$data['component'] = $this->load->view('backend/category_list', $componentData, TRUE);

// Load a component view into the main template

$this->load->view('backend/templates/template', $data);

回答:

Codeigniter Config类在类中加载配置变量,使它们不可变,而不调用$this->config->set_item()。要从控制器向config类中的数组添加变量,需要修改数组,然后将变量设置回配置类,然后才能从程序中的其他位置访问这些变量。

class Categories extends Backend_Controller{ 

function __construct(){

parent::__construct();

// Load dependencies

$head_meta = config_item('head_meta');

array_push($head_meta['end_scripts'], 'plugins/redactor.min.js', 'categories.js');

array_push($head_meta['stylesheets'], 'redactor.css');

$this->config->set_item('head_meta', $head_meta);

var_dump($this->config->get_item('head_meta'));

}

// THE REST OF THE CLASS ...

}

以上是 CodeIgniter将新值推入配置文件的数组 的全部内容, 来源链接: utcz.com/qa/266493.html

回到顶部