blob: c16d4b72917c79222a9f7026eea5b71e2b05c570 [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">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
adminb0dd10f2006-08-25 17:25:49 +00003<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Config Class</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>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<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>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Allard067f2cc2008-02-04 22:12:03 +000031<td><h1>CodeIgniter User Guide Version 1.6.1</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000033</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Jones7a9193a2008-01-21 18:39:20 +000043<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Config Class
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<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 +000048</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
59<h1>Config Class</h1>
60
61<p>The Config class provides a means to retrieve configuration preferences. These preferences can
62come from the default config file (<samp>application/config/config.php</samp>) or from your own custom config files.</p>
63
64<p class="important"><strong>Note:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
65
66
67<h2>Anatomy of a Config File</h2>
68
Derek Allardd2df9bc2007-04-15 17:41:17 +000069<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 +000070your text editor you'll see that config items are stored in an array called <var>$config</var>.</p>
71
72<p>You can add your own config items to
admine334c472006-10-21 19:44:22 +000073this file, or if you prefer to keep your configuration items separate (assuming you even need config items),
adminb0dd10f2006-08-25 17:25:49 +000074simply create your own file and save it in <dfn>config</dfn> folder.</p>
75
76<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 +000077an 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 +000078the array has the same name (assuming an array index is not named the same as another).</p>
79
80<h2>Loading a Config File</h2>
81
Derek Allardd2df9bc2007-04-15 17:41:17 +000082<p><strong>Note:</strong> CodeIgniter automatically loads the primary config file (<samp>application/config/config.php</samp>),
adminb0dd10f2006-08-25 17:25:49 +000083so you will only need to load a config file if you have created your own.</p>
84
85<p>There are two ways to load a config file:</p>
86
87<ol><li><strong>Manual Loading</strong>
88
89<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>
90
91<code>$this->config->load('<var>filename</var>');</code>
92
93<p>Where <var>filename</var> is the name of your config file, without the .php file extension.</p>
94
admineb6db842006-09-02 02:39:45 +000095<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
96you 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 +000097and 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 +000098
99<code>
100// Stored in an array with this prototype: $this->config['blog_settings'] = $config<br />
101$this->config->load('<var>blog_settings</var>', <kbd>TRUE</kbd>);</code>
102
103<p>Please see the section entitled <dfn>Fetching Config Items</dfn> below to learn how to retrieve config items set this way.</p>
104
105<p>The third parameter allows you to suppress errors in the event that a config file does not exist:</p>
106
107<code>$this->config->load('<var>blog_settings</var>', <dfn>FALSE</dfn>, <kbd>TRUE</kbd>);</code>
108
adminb0dd10f2006-08-25 17:25:49 +0000109</li>
Derek Allardc6441282007-07-04 23:54:32 +0000110<li><strong>Auto-loading</strong>
adminb0dd10f2006-08-25 17:25:49 +0000111
112<p>If you find that you need a particular config file globally, you can have it loaded automatically by the system. To do this,
113open the <strong>autoload.php</strong> file, located at <samp>application/config/autoload.php</samp>, and add your config file as
114indicated in the file.</p>
115</li>
116</ol>
117
118
119<h2>Fetching Config Items</h2>
120
admineb6db842006-09-02 02:39:45 +0000121<p>To retrieve an item from your config file, use the following function:</p>
adminb0dd10f2006-08-25 17:25:49 +0000122
123<code>$this->config->item('<var>item name</var>');</code>
124
125<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>
126
127<code>$lang = $this->config->item('language');</code>
128
129<p>The function returns FALSE (boolean) if the item you are trying to fetch does not exist.</p>
130
admineb6db842006-09-02 02:39:45 +0000131<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 +0000132you 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 +0000133
134<code>
135// Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"<br />
136$this->config->load('<var>blog_settings</var>', '<kbd>TRUE</kbd>');<br /><br />
137
138// Retrieve a config item named site_name contained within the blog_settings array<br />
139$site_name = $this->config->item('<dfn>site_name</dfn>', '<var>blog_settings</var>');<br /><br />
140
141// An alternate way to specify the same item:<br />
142$blog_config = $this->config->item('<var>blog_settings</var>');<br />
143$site_name = $blog_config['site_name'];</code>
144
adminb0dd10f2006-08-25 17:25:49 +0000145<h2>Setting a Config Item</h2>
146
admine7e1dcd2006-10-21 18:04:01 +0000147<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 +0000148
149<code>$this->config->set_item('<var>item_name</var>', '<var>item_value</var>');</code>
150
151<p>Where <var>item_name</var> is the $config array index you want to change, and <var>item_value</var> is its value.</p>
152
153
154<h2>Helper Functions</h2>
155
156<p>The config class has the following helper functions:</p>
157
158<h2>$this->config->site_url();</h2>
159<p>This function retrieves the URL to your site, along with the "index" value you've specified in the config file.</p>
160
161<h2>$this->config->system_url();</h2>
162<p>This function retrieves the URL to your <dfn>system folder</dfn>.</p>
163
164
165</div>
166<!-- END CONTENT -->
167
168
169<div id="footer">
170<p>
171Previous Topic:&nbsp;&nbsp;<a href="calendar.html">Calendaring Class</a>
172&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
173<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
174<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Rick Ellis325197e2006-11-20 17:29:05 +0000175Next Topic:&nbsp;&nbsp;<a href="../database/index.html">Database Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000176</p>
Derek Jones7a9193a2008-01-21 18:39:20 +0000177<p><a href="http://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 +0000178</div>
179
180</body>
181</html>