【问题标题】:How to override config's array within a controller in CodeIgniter?如何在 CodeIgniter 的控制器中覆盖配置数组?
【发布时间】:2013-08-06 20:08:10
【问题描述】:

我有一个文件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 的文档中,有一个示例可以像这样覆盖配置的值:$this->config->set_item('configItem', 'value');

但是,如果我的配置项是一个数组,应该怎么办?我试过$this->config->set_item('description', 'head_meta', 'NewValue'); 但没用

感谢您的建议

【问题讨论】:

    标签: php multidimensional-array codeigniter-2


    【解决方案1】:

    很遗憾,通过使用$this->config->set_item('item_name', 'item_value');,无法更改配置值中的特定数组项。

    你可能需要获取当前的配置值,修改它并重新设置:

    $this->config->set_item('head_meta', array_merge(
        $this->config->item('head_meta'), array('description' => 'newValue')
    ));
    

    【讨论】:

    • 哇!真的难以置信。无论如何谢谢@Hashem。
    猜你喜欢
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多