How to override config's array within a contro

2019-05-23 01:43发布

问题:

I have a file 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'
    )
);

I need to override that description inside my controller's function 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);

}

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');

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

Thanks for advices

回答1:

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')
));