admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
Derek Allard | afd99ac | 2008-01-19 19:59:14 +0000 | [diff] [blame] | 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 3 | <head>
|
| 4 |
|
Derek Allard | 8039d4c | 2008-05-31 02:47:56 +0000 | [diff] [blame] | 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
Derek Jones | fd93d22 | 2008-05-06 15:18:50 +0000 | [diff] [blame] | 6 | <title>Config Class : CodeIgniter User Guide</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 7 |
|
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 10 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 15 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 21 |
|
| 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>
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 27 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 28 | <div id="masthead">
|
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 30 | <tr>
|
Rick Ellis | 2594953 | 2008-08-26 19:48:08 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.7</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 33 | </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 Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | Config Class
|
| 46 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 47 | <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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 48 | </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
|
| 62 | come 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 Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 69 | <p>By default, CodeIgniter has a one primary config file, located at <samp>application/config/config.php</samp>. If you open the file using
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 70 | your 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
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 73 | this file, or if you prefer to keep your configuration items separate (assuming you even need config items),
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 74 | simply 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 Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 77 | an array called <var>$config</var>. CodeIgniter will intelligently manage these files so there will be no conflict even though
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 78 | the 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 Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 82 | <p><strong>Note:</strong> CodeIgniter automatically loads the primary config file (<samp>application/config/config.php</samp>),
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 83 | so 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 |
|
admin | eb6db84 | 2006-09-02 02:39:45 +0000 | [diff] [blame] | 95 | <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
|
| 96 | you have identically named array indexes in different config files. To avoid collisions you can set the second parameter to <kbd>TRUE</kbd>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 97 | and each config file will be stored in an array index corresponding to the name of the config file. Example:</p>
|
admin | eb6db84 | 2006-09-02 02:39:45 +0000 | [diff] [blame] | 98 |
|
| 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 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 109 | </li>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 110 | <li><strong>Auto-loading</strong>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 111 |
|
| 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,
|
| 113 | open the <strong>autoload.php</strong> file, located at <samp>application/config/autoload.php</samp>, and add your config file as
|
| 114 | indicated in the file.</p>
|
| 115 | </li>
|
| 116 | </ol>
|
| 117 |
|
| 118 |
|
| 119 | <h2>Fetching Config Items</h2>
|
| 120 |
|
admin | eb6db84 | 2006-09-02 02:39:45 +0000 | [diff] [blame] | 121 | <p>To retrieve an item from your config file, use the following function:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 122 |
|
| 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 |
|
admin | eb6db84 | 2006-09-02 02:39:45 +0000 | [diff] [blame] | 131 | <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 Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 132 | you can retrieve it by specifying the index name in the second parameter of the <kbd>$this->config->item()</kbd> function. Example:</p>
|
admin | eb6db84 | 2006-09-02 02:39:45 +0000 | [diff] [blame] | 133 |
|
| 134 | <code>
|
| 135 | // Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"<br />
|
Derek Allard | 9b808ef | 2008-03-23 13:07:45 +0000 | [diff] [blame] | 136 | $this->config->load('<var>blog_settings</var>', <kbd>TRUE</kbd>);<br /><br />
|
admin | eb6db84 | 2006-09-02 02:39:45 +0000 | [diff] [blame] | 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 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 145 | <h2>Setting a Config Item</h2>
|
| 146 |
|
admin | e7e1dcd | 2006-10-21 18:04:01 +0000 | [diff] [blame] | 147 | <p>If you would like to dynamically set a config item or change an existing one, you can so using:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 148 |
|
| 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>
|
| 171 | Previous Topic: <a href="calendar.html">Calendaring Class</a>
|
| 172 | ·
|
| 173 | <a href="#top">Top of Page</a> ·
|
| 174 | <a href="../index.html">User Guide Home</a> ·
|
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 175 | Next Topic: <a href="../database/index.html">Database Class</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 176 | </p>
|
Derek Jones | 0787043 | 2008-02-13 03:49:26 +0000 | [diff] [blame] | 177 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 178 | </div>
|
| 179 |
|
| 180 | </body>
|
| 181 | </html> |