blob: 08d9c290582f59f51734a34becd043df70327a85 [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
12.. contents:: Page Contents
13
14Anatomy of a Config File
15========================
16
17By default, CodeIgniter has one primary config file, located at
18application/config/config.php. If you open the file using your text
19editor you'll see that config items are stored in an array called
20$config.
21
22You can add your own config items to this file, or if you prefer to keep
23your configuration items separate (assuming you even need config items),
24simply create your own file and save it in config folder.
25
26.. note:: If you do create your own config files use the same format as
27 the primary one, storing your items in an array called $config.
28 CodeIgniter will intelligently manage these files so there will be no
29 conflict even though the array has the same name (assuming an array
30 index is not named the same as another).
31
32Loading a Config File
33=====================
34
35.. note::
36 CodeIgniter automatically loads the primary config file
37 (application/config/config.php), so you will only need to load a config
38 file if you have created your own.
39
40There are two ways to load a config file:
41
42Manual Loading
43**************
44
45To load one of your custom config files you will use the following
46function within the :doc:`controller </general/controllers>` that
47needs it::
48
49 $this->config->load('filename');
50
51Where filename is the name of your config file, without the .php file
52extension.
53
54If you need to load multiple config files normally they will be
55merged into one master config array. Name collisions can occur,
56however, if you have identically named array indexes in different
57config files. To avoid collisions you can set the second parameter to
58TRUE and each config file will be stored in an array index
59corresponding to the name of the config file. Example::
60
61 // Stored in an array with this prototype: $this->config['blog_settings'] = $config
62 $this->config->load('blog_settings', TRUE);
63
64Please see the section entitled Fetching Config Items below to learn
65how to retrieve config items set this way.
66
67The third parameter allows you to suppress errors in the event that a
68config file does not exist::
69
70 $this->config->load('blog_settings', FALSE, TRUE);
71
72Auto-loading
73************
74
75If you find that you need a particular config file globally, you can
76have it loaded automatically by the system. To do this, open the
77**autoload.php** file, located at application/config/autoload.php,
78and add your config file as indicated in the file.
79
80
81Fetching Config Items
82=====================
83
84To retrieve an item from your config file, use the following function::
85
86 $this->config->item('item name');
87
88Where item name is the $config array index you want to retrieve. For
89example, to fetch your language choice you'll do this::
90
91 $lang = $this->config->item('language');
92
93The function returns FALSE (boolean) if the item you are trying to fetch
94does not exist.
95
96If you are using the second parameter of the $this->config->load
97function in order to assign your config items to a specific index you
98can retrieve it by specifying the index name in the second parameter of
99the $this->config->item() function. Example::
100
101 // Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"
102 $this->config->load('blog_settings', TRUE);
103
104 // Retrieve a config item named site_name contained within the blog_settings array
105 $site_name = $this->config->item('site_name', 'blog_settings');
106
107 // An alternate way to specify the same item:
108 $blog_config = $this->config->item('blog_settings');
109 $site_name = $blog_config['site_name'];
110
111Setting a Config Item
112=====================
113
114If you would like to dynamically set a config item or change an existing
115one, you can do so using::
116
117 $this->config->set_item('item_name', 'item_value');
118
119Where item_name is the $config array index you want to change, and
120item_value is its value.
121
122.. _config-environments:
123
124Environments
125============
126
127You may load different configuration files depending on the current
128environment. The ENVIRONMENT constant is defined in index.php, and is
129described in detail in the :doc:`Handling
130Environments </general/environments>` section.
131
132To create an environment-specific configuration file, create or copy a
133configuration file in application/config/{ENVIRONMENT}/{FILENAME}.php
134
135For example, to create a production-only config.php, you would:
136
137#. Create the directory application/config/production/
138#. Copy your existing config.php into the above directory
139#. Edit application/config/production/config.php so it contains your
140 production settings
141
142When you set the ENVIRONMENT constant to 'production', the settings for
143your new production-only config.php will be loaded.
144
145You can place the following configuration files in environment-specific
146folders:
147
148- Default CodeIgniter configuration files
149- Your own custom configuration files
150
151.. note::
Thanasis Polychronakis991cfa22012-05-31 21:54:35 +0300152 CodeIgniter always loads the global config file first (i.e., the one in application/config/),
153 then tries to load the configuration files for the current environment.
154 This means you are not obligated to place **all** of your configuration files in an
155 environment folder. Only the files that change per environment. Additionally you don't
156 have to copy **all** the config items in the environment config file. Only the config items
157 that you wish to change for your environment. The config items declared in your environment
158 folders always overwrite those in your global config files.
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
160Helper Functions
161================
162
163The config class has the following helper functions:
164
165$this->config->site_url();
166***************************
167
168This function retrieves the URL to your site, along with the "index"
169value you've specified in the config file.
170
171$this->config->base_url();
172***************************
173
174This function retrieves the URL to your site, plus an optional path such
175as to a stylesheet or image.
176
177The two functions above are normally accessed via the corresponding
Derek Jonesa78c6e02012-07-04 06:52:19 -0700178functions in the :doc:`URL Helper </helpers/url_helper>`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
180$this->config->system_url();
181*****************************
182
183This function retrieves the URL to your system folder.