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">
|
| 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 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
admin | e0cd609 | 2006-10-20 01:00:31 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0b3</h1></td>
|
admin | 939906b | 2006-10-11 19:39:48 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Config 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 <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>
|
| 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
|
| 67 | come 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
|
| 75 | your 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
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 78 | 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] | 79 | simply 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
|
| 82 | an array called <var>$config</var>. Code Igniter will intelligently manage these files so there will be no conflict even though
|
| 83 | the 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 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 87 | <p><strong>Note:</strong> Code Igniter automatically loads the primary config file (<samp>application/config/config.php</samp>),
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 88 | so 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 |
|
admin | eb6db84 | 2006-09-02 02:39:45 +0000 | [diff] [blame] | 100 | <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
|
| 101 | you have identically named array indexes in different config files. To avoid collisions you can set the second parameter to <kbd>TRUE</kbd>
|
| 102 | and 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 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 116 | </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,
|
| 120 | open the <strong>autoload.php</strong> file, located at <samp>application/config/autoload.php</samp>, and add your config file as
|
| 121 | indicated in the file.</p>
|
| 122 | </li>
|
| 123 | </ol>
|
| 124 |
|
| 125 |
|
| 126 | <h2>Fetching Config Items</h2>
|
| 127 |
|
admin | eb6db84 | 2006-09-02 02:39:45 +0000 | [diff] [blame] | 128 | <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] | 129 |
|
| 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 |
|
admin | eb6db84 | 2006-09-02 02:39:45 +0000 | [diff] [blame] | 138 | <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
|
| 139 | you 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 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 152 | <h2>Setting a Config Item</h2>
|
| 153 |
|
admin | e7e1dcd | 2006-10-21 18:04:01 +0000 | [diff] [blame] | 154 | <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] | 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>
|
| 178 | Previous Topic: <a href="calendar.html">Calendaring Class</a>
|
| 179 | ·
|
| 180 | <a href="#top">Top of Page</a> ·
|
| 181 | <a href="../index.html">User Guide Home</a> ·
|
| 182 | Next Topic: <a href="database/index.html">Database Class</a>
|
| 183 | <p>
|
| 184 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 185 | </div>
|
| 186 |
|
| 187 | </body>
|
| 188 | </html> |