如何在CodeIgniter的控制器中覆盖配置的数组? [英] How to override config's array within a controller in CodeIgniter?
问题描述
我有一个文件 app / config / template.php
:
$config['head_meta'] = array(
'charset' => 'UTF-8',
'description' => '',
'keywords' => '',
'stylesheets' => array(
'template.css'
),
'scripts' => array(
'plugins/jquery-2.0.3.min.js',
'plugins/bootstrap.min.js'
),
'end_scripts' => array(
'template.js'
)
);
我需要在控制器功能 app / controllers / pages中覆盖该描述.php
:
function contact($pagename = 'Contact', $slug = 'contact'){
// load dependencies
$this->load->library('form_validation');
$this->lang->load($slug);
// page settings
$this->config->set_item('page_name', $this->lang->line('menu_subtitle_contact'));
$this->config->set_item('page_slug', $slug);
$description = $this->config->item('description', 'head_meta');
var_dump($description);
$data['view'] = $this->load->view($slug, '', TRUE);
$this->load->view('templates/default', $data);
}
我应该怎么做?在CI2的文档中,有一个示例可以覆盖config的值,例如: $ this-> config-> set_item('configItem','value');
How I suppose to do that ? In CI2's docs, there is an example to override config's value like this: $this->config->set_item('configItem', 'value');
但是如果我的配置项是数组,应该怎么办?我试过 $ this-> config-> set_item('description','head_meta','NewValue');
但没用
But how it should be if my config's item is an array ? I tried $this->config->set_item('description', 'head_meta', 'NewValue');
but it didn't work
谢谢您的建议
推荐答案
不幸的是,使用 $ this -> config-> set_item('item_name','item_value');
,无法在config值内更改特定的数组项。
Unfortunately, by using $this->config->set_item('item_name', 'item_value');
, it isn't possible to change a specific array item inside the config value.
您可能必须获取当前配置值,对其进行修改并再次设置:
You probably have to get the current config value, modify it and set it again:
$this->config->set_item('head_meta', array_merge(
$this->config->item('head_meta'), array('description' => 'newValue')
));
这篇关于如何在CodeIgniter的控制器中覆盖配置的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!