blob: 5ab85cc14c1dbe8e18c8c0beb658eb4544242263 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
Derek Allardd2df9bc2007-04-15 17:41:17 +00005<title>CodeIgniter User Guide</title>
adminb0dd10f2006-08-25 17:25:49 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000013<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminb0dd10f2006-08-25 17:25:49 +000016 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000026
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
Derek Allardd2df9bc2007-04-15 17:41:17 +000036<td><h1>CodeIgniter User Guide Version 1.5.3</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000038</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Config Class
51</td>
Derek Allardbc030912007-06-24 18:25:29 +000052<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminb0dd10f2006-08-25 17:25:49 +000053</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
64<h1>Config Class</h1>
65
66<p>The Config class provides a means to retrieve configuration preferences. These preferences can
67come from the default config file (<samp>application/config/config.php</samp>) or from your own custom config files.</p>
68
69<p class="important"><strong>Note:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
70
71
72<h2>Anatomy of a Config File</h2>
73
Derek Allardd2df9bc2007-04-15 17:41:17 +000074<p>By default, CodeIgniter has a one primary config file, located at <samp>application/config/config.php</samp>. If you open the file using
adminb0dd10f2006-08-25 17:25:49 +000075your text editor you'll see that config items are stored in an array called <var>$config</var>.</p>
76
77<p>You can add your own config items to
admine334c472006-10-21 19:44:22 +000078this file, or if you prefer to keep your configuration items separate (assuming you even need config items),
adminb0dd10f2006-08-25 17:25:49 +000079simply create your own file and save it in <dfn>config</dfn> folder.</p>
80
81<p><strong>Note:</strong> If you do create your own config files use the same format as the primary one, storing your items in
Derek Allardd2df9bc2007-04-15 17:41:17 +000082an array called <var>$config</var>. CodeIgniter will intelligently manage these files so there will be no conflict even though
adminb0dd10f2006-08-25 17:25:49 +000083the array has the same name (assuming an array index is not named the same as another).</p>
84
85<h2>Loading a Config File</h2>
86
Derek Allardd2df9bc2007-04-15 17:41:17 +000087<p><strong>Note:</strong> CodeIgniter automatically loads the primary config file (<samp>application/config/config.php</samp>),
adminb0dd10f2006-08-25 17:25:49 +000088so you will only need to load a config file if you have created your own.</p>
89
90<p>There are two ways to load a config file:</p>
91
92<ol><li><strong>Manual Loading</strong>
93
94<p>To load one of your custom config files you will use the following function within the <a href="../general/controllers.html">controller</a> that needs it:</p>
95
96<code>$this->config->load('<var>filename</var>');</code>
97
98<p>Where <var>filename</var> is the name of your config file, without the .php file extension.</p>
99
admineb6db842006-09-02 02:39:45 +0000100<p>If you need to load multiple config files normally they will be merged into one master config array. Name collisions can occur, however, if
101you have identically named array indexes in different config files. To avoid collisions you can set the second parameter to <kbd>TRUE</kbd>
Derek Allardc6441282007-07-04 23:54:32 +0000102and each config file will be stored in an array index corresponding to the name of the config file. Example:</p>
admineb6db842006-09-02 02:39:45 +0000103
104<code>
105// Stored in an array with this prototype: $this->config['blog_settings'] = $config<br />
106$this->config->load('<var>blog_settings</var>', <kbd>TRUE</kbd>);</code>
107
108<p>Please see the section entitled <dfn>Fetching Config Items</dfn> below to learn how to retrieve config items set this way.</p>
109
110<p>The third parameter allows you to suppress errors in the event that a config file does not exist:</p>
111
112<code>$this->config->load('<var>blog_settings</var>', <dfn>FALSE</dfn>, <kbd>TRUE</kbd>);</code>
113
adminb0dd10f2006-08-25 17:25:49 +0000114</li>
Derek Allardc6441282007-07-04 23:54:32 +0000115<li><strong>Auto-loading</strong>
adminb0dd10f2006-08-25 17:25:49 +0000116
117<p>If you find that you need a particular config file globally, you can have it loaded automatically by the system. To do this,
118open the <strong>autoload.php</strong> file, located at <samp>application/config/autoload.php</samp>, and add your config file as
119indicated in the file.</p>
120</li>
121</ol>
122
123
124<h2>Fetching Config Items</h2>
125
admineb6db842006-09-02 02:39:45 +0000126<p>To retrieve an item from your config file, use the following function:</p>
adminb0dd10f2006-08-25 17:25:49 +0000127
128<code>$this->config->item('<var>item name</var>');</code>
129
130<p>Where <var>item name</var> is the $config array index you want to retrieve. For example, to fetch your language choice you'll do this:</p>
131
132<code>$lang = $this->config->item('language');</code>
133
134<p>The function returns FALSE (boolean) if the item you are trying to fetch does not exist.</p>
135
admineb6db842006-09-02 02:39:45 +0000136<p>If you are using the second parameter of the <kbd>$this->config->load</kbd> function in order to assign your config items to a specific index
Derek Allardc6441282007-07-04 23:54:32 +0000137you can retrieve it by specifying the index name in the second parameter of the <kbd>$this->config->item()</kbd> function. Example:</p>
admineb6db842006-09-02 02:39:45 +0000138
139<code>
140// Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"<br />
141$this->config->load('<var>blog_settings</var>', '<kbd>TRUE</kbd>');<br /><br />
142
143// Retrieve a config item named site_name contained within the blog_settings array<br />
144$site_name = $this->config->item('<dfn>site_name</dfn>', '<var>blog_settings</var>');<br /><br />
145
146// An alternate way to specify the same item:<br />
147$blog_config = $this->config->item('<var>blog_settings</var>');<br />
148$site_name = $blog_config['site_name'];</code>
149
adminb0dd10f2006-08-25 17:25:49 +0000150<h2>Setting a Config Item</h2>
151
admine7e1dcd2006-10-21 18:04:01 +0000152<p>If you would like to dynamically set a config item or change an existing one, you can so using:</p>
adminb0dd10f2006-08-25 17:25:49 +0000153
154<code>$this->config->set_item('<var>item_name</var>', '<var>item_value</var>');</code>
155
156<p>Where <var>item_name</var> is the $config array index you want to change, and <var>item_value</var> is its value.</p>
157
158
159<h2>Helper Functions</h2>
160
161<p>The config class has the following helper functions:</p>
162
163<h2>$this->config->site_url();</h2>
164<p>This function retrieves the URL to your site, along with the "index" value you've specified in the config file.</p>
165
166<h2>$this->config->system_url();</h2>
167<p>This function retrieves the URL to your <dfn>system folder</dfn>.</p>
168
169
170</div>
171<!-- END CONTENT -->
172
173
174<div id="footer">
175<p>
176Previous Topic:&nbsp;&nbsp;<a href="calendar.html">Calendaring Class</a>
177&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
178<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
179<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Rick Ellis325197e2006-11-20 17:29:05 +0000180Next Topic:&nbsp;&nbsp;<a href="../database/index.html">Database Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000181</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000182<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000183</div>
184
185</body>
186</html>