blob: 3138e3403b1b9e8f764129171ae70651cb6ebae4 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001############
2Config Class
3############
4
5The Config class provides a means to retrieve configuration preferences.
6These 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 Jones912ee4c2013-07-21 23:52:27 -070012.. contents::
13 :local:
14
15.. raw:: html
16
17 <div class="custom-index container"></div>
18
19*****************************
20Working with the Config Class
21*****************************
Derek Jones8ede1a22011-10-05 13:34:52 -050022
23Anatomy of a Config File
24========================
25
26By default, CodeIgniter has one primary config file, located at
27application/config/config.php. If you open the file using your text
28editor you'll see that config items are stored in an array called
29$config.
30
31You can add your own config items to this file, or if you prefer to keep
32your configuration items separate (assuming you even need config items),
33simply 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
41Loading 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
49There are two ways to load a config file:
50
51Manual Loading
52**************
53
54To load one of your custom config files you will use the following
55function within the :doc:`controller </general/controllers>` that
56needs it::
57
58 $this->config->load('filename');
59
60Where filename is the name of your config file, without the .php file
61extension.
62
63If you need to load multiple config files normally they will be
64merged into one master config array. Name collisions can occur,
65however, if you have identically named array indexes in different
66config files. To avoid collisions you can set the second parameter to
67TRUE and each config file will be stored in an array index
68corresponding 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
73Please see the section entitled Fetching Config Items below to learn
74how to retrieve config items set this way.
75
76The third parameter allows you to suppress errors in the event that a
77config file does not exist::
78
79 $this->config->load('blog_settings', FALSE, TRUE);
80
81Auto-loading
82************
83
84If you find that you need a particular config file globally, you can
85have it loaded automatically by the system. To do this, open the
86**autoload.php** file, located at application/config/autoload.php,
87and add your config file as indicated in the file.
88
89
90Fetching Config Items
91=====================
92
93To retrieve an item from your config file, use the following function::
94
95 $this->config->item('item name');
96
97Where item name is the $config array index you want to retrieve. For
98example, to fetch your language choice you'll do this::
99
100 $lang = $this->config->item('language');
101
vlakoff184cf1b2013-07-24 03:43:39 +0200102The function returns NULL if the item you are trying to fetch
Derek Jones8ede1a22011-10-05 13:34:52 -0500103does not exist.
104
105If you are using the second parameter of the $this->config->load
106function in order to assign your config items to a specific index you
107can retrieve it by specifying the index name in the second parameter of
108the $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
120Setting a Config Item
121=====================
122
123If you would like to dynamically set a config item or change an existing
124one, you can do so using::
125
126 $this->config->set_item('item_name', 'item_value');
127
128Where item_name is the $config array index you want to change, and
129item_value is its value.
130
131.. _config-environments:
132
133Environments
134============
135
136You may load different configuration files depending on the current
137environment. The ENVIRONMENT constant is defined in index.php, and is
138described in detail in the :doc:`Handling
139Environments </general/environments>` section.
140
141To create an environment-specific configuration file, create or copy a
142configuration file in application/config/{ENVIRONMENT}/{FILENAME}.php
143
144For 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
151When you set the ENVIRONMENT constant to 'production', the settings for
152your new production-only config.php will be loaded.
153
154You can place the following configuration files in environment-specific
155folders:
156
157- Default CodeIgniter configuration files
158- Your own custom configuration files
159
160.. note::
Thanasis Polychronakis991cfa22012-05-31 21:54:35 +0300161 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 Jones8ede1a22011-10-05 13:34:52 -0500168
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Derek Jones912ee4c2013-07-21 23:52:27 -0700170***************
171Class Reference
172***************
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200174.. php:class:: CI_Config
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Derek Jones912ee4c2013-07-21 23:52:27 -0700176 .. attribute:: $config
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Derek Jones912ee4c2013-07-21 23:52:27 -0700178 Array of all loaded config values
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Derek Jones912ee4c2013-07-21 23:52:27 -0700180 .. attribute:: $is_loaded
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Derek Jones912ee4c2013-07-21 23:52:27 -0700182 Array of all loaded config files
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200185 .. php:method:: item($item[, $index=''])
Derek Jones912ee4c2013-07-21 23:52:27 -0700186
Andrey Andreev28c2c972014-02-08 04:27:48 +0200187 :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 Jones912ee4c2013-07-21 23:52:27 -0700191
192 Fetch a config file item.
193
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200194 .. php:method:: set_item($item, $value)
Derek Jones912ee4c2013-07-21 23:52:27 -0700195
Andrey Andreev28c2c972014-02-08 04:27:48 +0200196 :param string $item: Config item name
197 :param string $value: Config item value
198 :rtype: void
Derek Jones912ee4c2013-07-21 23:52:27 -0700199
200 Sets a config file item to the specified value.
201
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200202 .. php:method:: slash_item($item)
Derek Jones912ee4c2013-07-21 23:52:27 -0700203
Andrey Andreev28c2c972014-02-08 04:27:48 +0200204 :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 Jones912ee4c2013-07-21 23:52:27 -0700207
Andrey Andreev28c2c972014-02-08 04:27:48 +0200208 This method is identical to ``item()``, except it appends a forward
Derek Jones912ee4c2013-07-21 23:52:27 -0700209 slash to the end of the item, if it exists.
210
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200211 .. php:method:: load([$file = ''[, $use_sections = FALSE[, $fail_gracefully = FALSE]]])
Derek Jones912ee4c2013-07-21 23:52:27 -0700212
Andrey Andreev28c2c972014-02-08 04:27:48 +0200213 :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 Jones912ee4c2013-07-21 23:52:27 -0700218
219 Loads a configuration file.
220
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200221 .. php:method:: site_url()
Derek Jones912ee4c2013-07-21 23:52:27 -0700222
Andrey Andreev28c2c972014-02-08 04:27:48 +0200223 :returns: Site URL
224 :rtype: string
Derek Jones912ee4c2013-07-21 23:52:27 -0700225
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 Andreevcd3d9db2015-02-02 13:41:01 +0200232 .. php:method:: base_url()
Derek Jones912ee4c2013-07-21 23:52:27 -0700233
Andrey Andreev28c2c972014-02-08 04:27:48 +0200234 :returns: Base URL
235 :rtype: string
Derek Jones912ee4c2013-07-21 23:52:27 -0700236
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 Andreevcd3d9db2015-02-02 13:41:01 +0200243 .. php:method:: system_url()
Derek Jones912ee4c2013-07-21 23:52:27 -0700244
Andrey Andreev28c2c972014-02-08 04:27:48 +0200245 :returns: URL pointing at your CI system/ directory
246 :rtype: string
Derek Jones912ee4c2013-07-21 23:52:27 -0700247
Andrey Andreeve4e10912014-02-08 19:58:48 +0200248 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.