Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 | <title>Config Class : CodeIgniter User Guide</title> |
| 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 | |
| 11 | <script type="text/javascript" src="../nav/nav.js"></script> |
| 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script> |
| 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script> |
| 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script> |
| 15 | |
| 16 | <meta http-equiv='expires' content='-1' /> |
| 17 | <meta http-equiv= 'pragma' content='no-cache' /> |
| 18 | <meta name='robots' content='all' /> |
| 19 | <meta name='author' content='ExpressionEngine Dev Team' /> |
| 20 | <meta name='description' content='CodeIgniter User Guide' /> |
| 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> |
| 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> |
| 28 | <div id="masthead"> |
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 30 | <tr> |
Derek Jones | bbedc76 | 2009-09-11 14:47:37 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.7.2</h1></td> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> |
| 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"> |
| 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> › |
| 44 | <a href="../index.html">User Guide Home</a> › |
| 45 | Config Class |
| 46 | </td> |
| 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> |
| 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 | |
| 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 |
| 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 |
| 73 | this file, or if you prefer to keep your configuration items separate (assuming you even need config items), |
| 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 |
| 77 | an array called <var>$config</var>. CodeIgniter will intelligently manage these files so there will be no conflict even though |
| 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 | |
| 82 | <p><strong>Note:</strong> CodeIgniter automatically loads the primary config file (<samp>application/config/config.php</samp>), |
| 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 | |
| 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> |
| 97 | and each config file will be stored in an array index corresponding to the name of the config file. Example:</p> |
| 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 | |
| 109 | </li> |
| 110 | <li><strong>Auto-loading</strong> |
| 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 | |
| 121 | <p>To retrieve an item from your config file, use the following function:</p> |
| 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 | |
| 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 |
| 132 | you can retrieve it by specifying the index name in the second parameter of the <kbd>$this->config->item()</kbd> function. Example:</p> |
| 133 | |
| 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 | |
| 145 | <h2>Setting a Config Item</h2> |
| 146 | |
| 147 | <p>If you would like to dynamically set a config item or change an existing one, you can so using:</p> |
| 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> · |
| 175 | Next Topic: <a href="../database/index.html">Database Class</a> |
| 176 | </p> |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 177 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2010 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | </div> |
| 179 | |
| 180 | </body> |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 181 | </html> |