Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############ |
| 2 | Config Class |
| 3 | ############ |
| 4 | |
| 5 | The Config class provides a means to retrieve configuration preferences. |
| 6 | These preferences can come from the default config file |
| 7 | (application/config/config.php) or from your own custom config files. |
| 8 | |
| 9 | .. note:: This class is initialized automatically by the system so there |
| 10 | is no need to do it manually. |
| 11 | |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 12 | .. contents:: |
| 13 | :local: |
| 14 | |
| 15 | .. raw:: html |
| 16 | |
| 17 | <div class="custom-index container"></div> |
| 18 | |
| 19 | ***************************** |
| 20 | Working with the Config Class |
| 21 | ***************************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | |
| 23 | Anatomy of a Config File |
| 24 | ======================== |
| 25 | |
| 26 | By default, CodeIgniter has one primary config file, located at |
| 27 | application/config/config.php. If you open the file using your text |
| 28 | editor you'll see that config items are stored in an array called |
| 29 | $config. |
| 30 | |
| 31 | You can add your own config items to this file, or if you prefer to keep |
| 32 | your configuration items separate (assuming you even need config items), |
| 33 | simply create your own file and save it in config folder. |
| 34 | |
| 35 | .. note:: If you do create your own config files use the same format as |
| 36 | the primary one, storing your items in an array called $config. |
| 37 | CodeIgniter will intelligently manage these files so there will be no |
| 38 | conflict even though the array has the same name (assuming an array |
| 39 | index is not named the same as another). |
| 40 | |
| 41 | Loading a Config File |
| 42 | ===================== |
| 43 | |
| 44 | .. note:: |
| 45 | CodeIgniter automatically loads the primary config file |
| 46 | (application/config/config.php), so you will only need to load a config |
| 47 | file if you have created your own. |
| 48 | |
| 49 | There are two ways to load a config file: |
| 50 | |
| 51 | Manual Loading |
| 52 | ************** |
| 53 | |
| 54 | To load one of your custom config files you will use the following |
| 55 | function within the :doc:`controller </general/controllers>` that |
| 56 | needs it:: |
| 57 | |
| 58 | $this->config->load('filename'); |
| 59 | |
| 60 | Where filename is the name of your config file, without the .php file |
| 61 | extension. |
| 62 | |
| 63 | If you need to load multiple config files normally they will be |
| 64 | merged into one master config array. Name collisions can occur, |
| 65 | however, if you have identically named array indexes in different |
| 66 | config files. To avoid collisions you can set the second parameter to |
| 67 | TRUE and each config file will be stored in an array index |
| 68 | corresponding to the name of the config file. Example:: |
| 69 | |
| 70 | // Stored in an array with this prototype: $this->config['blog_settings'] = $config |
| 71 | $this->config->load('blog_settings', TRUE); |
| 72 | |
| 73 | Please see the section entitled Fetching Config Items below to learn |
| 74 | how to retrieve config items set this way. |
| 75 | |
| 76 | The third parameter allows you to suppress errors in the event that a |
| 77 | config file does not exist:: |
| 78 | |
| 79 | $this->config->load('blog_settings', FALSE, TRUE); |
| 80 | |
| 81 | Auto-loading |
| 82 | ************ |
| 83 | |
| 84 | If you find that you need a particular config file globally, you can |
| 85 | have it loaded automatically by the system. To do this, open the |
| 86 | **autoload.php** file, located at application/config/autoload.php, |
| 87 | and add your config file as indicated in the file. |
| 88 | |
| 89 | |
| 90 | Fetching Config Items |
| 91 | ===================== |
| 92 | |
| 93 | To retrieve an item from your config file, use the following function:: |
| 94 | |
ftwbzhao | c836f2a | 2015-05-07 18:05:33 +0800 | [diff] [blame^] | 95 | $this->config->item('item_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 96 | |
ftwbzhao | c836f2a | 2015-05-07 18:05:33 +0800 | [diff] [blame^] | 97 | Where item_name is the $config array index you want to retrieve. For |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | example, to fetch your language choice you'll do this:: |
| 99 | |
| 100 | $lang = $this->config->item('language'); |
| 101 | |
vlakoff | 184cf1b | 2013-07-24 03:43:39 +0200 | [diff] [blame] | 102 | The function returns NULL if the item you are trying to fetch |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | does not exist. |
| 104 | |
| 105 | If you are using the second parameter of the $this->config->load |
| 106 | function in order to assign your config items to a specific index you |
| 107 | can retrieve it by specifying the index name in the second parameter of |
| 108 | the $this->config->item() function. Example:: |
| 109 | |
| 110 | // Loads a config file named blog_settings.php and assigns it to an index named "blog_settings" |
| 111 | $this->config->load('blog_settings', TRUE); |
| 112 | |
| 113 | // Retrieve a config item named site_name contained within the blog_settings array |
| 114 | $site_name = $this->config->item('site_name', 'blog_settings'); |
| 115 | |
| 116 | // An alternate way to specify the same item: |
| 117 | $blog_config = $this->config->item('blog_settings'); |
| 118 | $site_name = $blog_config['site_name']; |
| 119 | |
| 120 | Setting a Config Item |
| 121 | ===================== |
| 122 | |
| 123 | If you would like to dynamically set a config item or change an existing |
| 124 | one, you can do so using:: |
| 125 | |
| 126 | $this->config->set_item('item_name', 'item_value'); |
| 127 | |
| 128 | Where item_name is the $config array index you want to change, and |
| 129 | item_value is its value. |
| 130 | |
| 131 | .. _config-environments: |
| 132 | |
| 133 | Environments |
| 134 | ============ |
| 135 | |
| 136 | You may load different configuration files depending on the current |
| 137 | environment. The ENVIRONMENT constant is defined in index.php, and is |
| 138 | described in detail in the :doc:`Handling |
| 139 | Environments </general/environments>` section. |
| 140 | |
| 141 | To create an environment-specific configuration file, create or copy a |
| 142 | configuration file in application/config/{ENVIRONMENT}/{FILENAME}.php |
| 143 | |
| 144 | For example, to create a production-only config.php, you would: |
| 145 | |
| 146 | #. Create the directory application/config/production/ |
| 147 | #. Copy your existing config.php into the above directory |
| 148 | #. Edit application/config/production/config.php so it contains your |
| 149 | production settings |
| 150 | |
| 151 | When you set the ENVIRONMENT constant to 'production', the settings for |
| 152 | your new production-only config.php will be loaded. |
| 153 | |
| 154 | You can place the following configuration files in environment-specific |
| 155 | folders: |
| 156 | |
| 157 | - Default CodeIgniter configuration files |
| 158 | - Your own custom configuration files |
| 159 | |
| 160 | .. note:: |
Thanasis Polychronakis | 991cfa2 | 2012-05-31 21:54:35 +0300 | [diff] [blame] | 161 | CodeIgniter always loads the global config file first (i.e., the one in application/config/), |
| 162 | then tries to load the configuration files for the current environment. |
| 163 | This means you are not obligated to place **all** of your configuration files in an |
| 164 | environment folder. Only the files that change per environment. Additionally you don't |
| 165 | have to copy **all** the config items in the environment config file. Only the config items |
| 166 | that you wish to change for your environment. The config items declared in your environment |
| 167 | folders always overwrite those in your global config files. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 169 | |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 170 | *************** |
| 171 | Class Reference |
| 172 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 174 | .. php:class:: CI_Config |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 175 | |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 176 | .. attribute:: $config |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 177 | |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 178 | Array of all loaded config values |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 179 | |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 180 | .. attribute:: $is_loaded |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 181 | |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 182 | Array of all loaded config files |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 183 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 184 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 185 | .. php:method:: item($item[, $index='']) |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 186 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 187 | :param string $item: Config item name |
| 188 | :param string $index: Index name |
| 189 | :returns: Config item value or NULL if not found |
| 190 | :rtype: mixed |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 191 | |
| 192 | Fetch a config file item. |
| 193 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 194 | .. php:method:: set_item($item, $value) |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 195 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 196 | :param string $item: Config item name |
| 197 | :param string $value: Config item value |
| 198 | :rtype: void |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 199 | |
| 200 | Sets a config file item to the specified value. |
| 201 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 202 | .. php:method:: slash_item($item) |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 203 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 204 | :param string $item: config item name |
| 205 | :returns: Config item value with a trailing forward slash or NULL if not found |
| 206 | :rtype: mixed |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 207 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 208 | This method is identical to ``item()``, except it appends a forward |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 209 | slash to the end of the item, if it exists. |
| 210 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 211 | .. php:method:: load([$file = ''[, $use_sections = FALSE[, $fail_gracefully = FALSE]]]) |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 212 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 213 | :param string $file: Configuration file name |
| 214 | :param bool $use_sections: Whether config values shoud be loaded into their own section (index of the main config array) |
| 215 | :param bool $fail_gracefully: Whether to return FALSE or to display an error message |
| 216 | :returns: TRUE on success, FALSE on failure |
| 217 | :rtype: bool |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 218 | |
| 219 | Loads a configuration file. |
| 220 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 221 | .. php:method:: site_url() |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 222 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 223 | :returns: Site URL |
| 224 | :rtype: string |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 225 | |
| 226 | This method retrieves the URL to your site, along with the "index" value |
| 227 | you've specified in the config file. |
| 228 | |
| 229 | This method is normally accessed via the corresponding functions in the |
| 230 | :doc:`URL Helper </helpers/url_helper>`. |
| 231 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 232 | .. php:method:: base_url() |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 233 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 234 | :returns: Base URL |
| 235 | :rtype: string |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 236 | |
| 237 | This method retrieves the URL to your site, plus an optional path such |
| 238 | as to a stylesheet or image. |
| 239 | |
| 240 | This method is normally accessed via the corresponding functions in the |
| 241 | :doc:`URL Helper </helpers/url_helper>`. |
| 242 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 243 | .. php:method:: system_url() |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 244 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 245 | :returns: URL pointing at your CI system/ directory |
| 246 | :rtype: string |
Derek Jones | 912ee4c | 2013-07-21 23:52:27 -0700 | [diff] [blame] | 247 | |
Andrey Andreev | e4e1091 | 2014-02-08 19:58:48 +0200 | [diff] [blame] | 248 | This method retrieves the URL to your CodeIgniter system/ directory. |
| 249 | |
| 250 | .. note:: This method is DEPRECATED because it encourages usage of |
| 251 | insecure coding practices. Your *system/* directory shouldn't |
| 252 | be publicly accessible. |