CI 的配置文件統一放在 application/config/
目錄下面,框架有一個默認的主配置文件 application/config/config.php
。其部份內容以下:php
<?php $config['uri_protocol'] = 'REQUEST_URI'; // ... $config['charset'] = 'UTF-8'; // ... $config['subclass_prefix'] = 'My_';
能夠看到全部的配置信息都放在 $config
數組裏。框架默認會加載這個配置文件,因此使用時直接用 item()
調用:數組
<?php $this->config->item('uri_protocol'); // 'REQUEST_URI'
若是你不想使用默認的配置文件,而是本身建立一個新的配置文件,那也是能夠的。在 application/config/
目錄下面建立一個 custom.php
:app
<?php $config['index_page'] = 'welcome';
使用時,須要先加載 custom.php
文件,而後獲取配置內容:框架
<?php $this->config->load('custom'); $index_page = $this->config->item('index_page'); // 'welcome'
從前面兩個例子中能夠看到配置信息都是 $config
數組的鍵指定的,那麼是否能夠自定義一個變量來指定配置信息呢?答案是'''不能夠''',不管是系統的 config.php
仍是自定義的配置文件,都必須在 $config
數組中定義配置項,由於 CI Config 在加載配置文件時會檢查是否含有 $config
數組,若是沒有,就報錯:'Your '.$file_path.' file does not appear to contain a valid configuration array.'
。this
當加載多個配置文件時,這些配置文件中的 $config
會合並,因此若是在不一樣的配置文件中若是有相同的鍵的話,就會產生衝突(先加載的配置會被後加載的配置覆蓋)。這能夠經過指定 load()
的第二個參數來解決。code
假設如今有兩個配置文件:custom1.php
和 custom2.php
,它們的內容以下:blog
// custom1.php $config['index_page'] = 'welcome1'; // custom2.php $config['index_page'] = 'welcome2';
在加載時指定 load()
第二個參數爲 TRUE,來分別保存配置項的值到不一樣的數組中(而不是原來的的 $config
):get
$this->config->load('custom1.php', TRUE); $this->config->load('custom2.php', TRUE);
而後在獲取配置項時,在 item()
第二個參數指定配置文件名就能夠正確獲取到配置項了:博客
$this->config->item('index_page', 'custom1'); // 'welcome1' $this->config->item('index_page', 'custom2'); // 'welcome2'
PS - 我的博客連接:CodeIgnitor 配置類的使用it