blob: ea3285b4a59eb6fdf84d7c3c64c65bc10d8d50ca [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
5<title>Code Igniter User Guide</title>
6
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
10<script type="text/javascript" src="../scripts/nav.js"></script>
11<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
12<script type="text/javascript" src="../scripts/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 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' />
25<meta name='description' content='Code Igniter User Guide' />
26
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>
admin41a16852006-09-26 18:17:19 +000036<td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
adminb0dd10f2006-08-25 17:25:49 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</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">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Config Class
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
53</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
74<p>By default, Code Igniter has a one primary config file, located at <samp>application/config/config.php</samp>. If you open the file using
75your 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
78this file, or if you prefer to keep your configuration items separate (assuming you even need config items),
79simply 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
82an array called <var>$config</var>. Code Igniter will intelligently manage these files so there will be no conflict even though
83the 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
87<p><strong>Note:</strong> Code Igniter automatically loads the primary config file (<samp>application/config/config.php</samp>),
88so 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>
102and each config file will be stored in an array index corresponding to the name of the config file. Example:
103
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
114
115
adminb0dd10f2006-08-25 17:25:49 +0000116</li>
117<li><strong>Auto-loading</strong></li>
118
119<p>If you find that you need a particular config file globally, you can have it loaded automatically by the system. To do this,
120open the <strong>autoload.php</strong> file, located at <samp>application/config/autoload.php</samp>, and add your config file as
121indicated in the file.</p>
122</li>
123</ol>
124
125
126<h2>Fetching Config Items</h2>
127
admineb6db842006-09-02 02:39:45 +0000128<p>To retrieve an item from your config file, use the following function:</p>
adminb0dd10f2006-08-25 17:25:49 +0000129
130<code>$this->config->item('<var>item name</var>');</code>
131
132<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>
133
134<code>$lang = $this->config->item('language');</code>
135
136<p>The function returns FALSE (boolean) if the item you are trying to fetch does not exist.</p>
137
admineb6db842006-09-02 02:39:45 +0000138<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
139you can retrieve it by specifying the index name in the second parameter of the <kbd>$this->config->item()</kbd> function. Example:
140
141<code>
142// Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"<br />
143$this->config->load('<var>blog_settings</var>', '<kbd>TRUE</kbd>');<br /><br />
144
145// Retrieve a config item named site_name contained within the blog_settings array<br />
146$site_name = $this->config->item('<dfn>site_name</dfn>', '<var>blog_settings</var>');<br /><br />
147
148// An alternate way to specify the same item:<br />
149$blog_config = $this->config->item('<var>blog_settings</var>');<br />
150$site_name = $blog_config['site_name'];</code>
151
adminb0dd10f2006-08-25 17:25:49 +0000152<h2>Setting a Config Item</h2>
153
154<p>If you would like to dynamically set a config item or change an existing one, you can so so using:</p>
155
156<code>$this->config->set_item('<var>item_name</var>', '<var>item_value</var>');</code>
157
158<p>Where <var>item_name</var> is the $config array index you want to change, and <var>item_value</var> is its value.</p>
159
160
161<h2>Helper Functions</h2>
162
163<p>The config class has the following helper functions:</p>
164
165<h2>$this->config->site_url();</h2>
166<p>This function retrieves the URL to your site, along with the "index" value you've specified in the config file.</p>
167
168<h2>$this->config->system_url();</h2>
169<p>This function retrieves the URL to your <dfn>system folder</dfn>.</p>
170
171
172</div>
173<!-- END CONTENT -->
174
175
176<div id="footer">
177<p>
178Previous Topic:&nbsp;&nbsp;<a href="calendar.html">Calendaring Class</a>
179&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
180<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
181<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
182Next Topic:&nbsp;&nbsp;<a href="database/index.html">Database Class</a>
183<p>
184<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
185</div>
186
187</body>
188</html>