Blog Details

Config Class

<h2>* Config Class :-</h2>
<ul>
<li>The Config class provides a means to retrieve configuration preferences.</li>
<li>These preferences can come from the default config file (<strong><strong><strong><strong><strong><strong><strong><strong>
<samp>application/config/config.php)or from your own custom config files.</samp></strong></strong></strong></strong></strong></strong></strong></strong>&nbsp;</li>
</ul>
<h2>* Loading a Config File :-</h2>
&nbsp;
<ul>
<li>There are <strong>two ways</strong> to load a config file:</li>
</ul>
&nbsp;
<h4><strong>1) Manual Loading :-</strong></h4>
&nbsp;
<ul>
<li>To load one of your custom config files you will use the following function within the controller that needs it:</li>
<li><strong>$this-&gt;config-&gt;load('<var>filename</var>');</strong></li>
<li>Where <var>filename</var> is the name of your config file, without the .php file extension.</li>
</ul>
&nbsp;
<h4><strong>2) Auto-loading :-</strong></h4>
&nbsp;
<ul>
<li>If you find that you need a particular config file globally, you can have it loaded automatically by the system. To do this, open the <strong>autoload.php</strong> file, located at <strong><strong><strong><strong><strong><strong><strong><strong>
<samp>aplpication/config/autoload.php , and add your config file as indicated in the file.</samp></strong></strong></strong></strong></strong></strong></strong></strong>&nbsp;</li>
</ul>
<h2>* Fetching Config Items :-</h2>
&nbsp;
<ul>
<li>To retrieve an item from your config file, use the following function:</li>
<li><strong>$this-&gt;config-&gt;item('<var>item name</var>');</strong></li>
<li>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:</li>
<li><strong>$lang = $this-&gt;config-&gt;item('language');</strong></li>
<li>The function returns FALSE (boolean) if the item you are trying to fetch does not exist.</li>
<li>If you are using the second parameter of the <kbd>$this-&gt;config-&gt;load</kbd> function in order to assign your config items to a specific index you can retrieve it by specifying the index name in the second parameter of the <kbd>$this-&gt;config-&gt;item()</kbd> function.</li>
</ul>
&nbsp;
<strong>Example :</strong>
&nbsp;
/ Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"
$this-&gt;config-&gt;load('<var>blog_settings</var>', <kbd>TRUE</kbd>);
// Retrieve a config item named site_name contained within the blog_settings array
$site_name = $this-&gt;config-&gt;item('<dfn>site_name</dfn>', '<var>blog_settings</var>');
// An alternate way to specify the same item:
$blog_config = $this-&gt;config-&gt;item('<var>blog_settings</var>');
$site_name = $blog_config['site_name'];
&nbsp;
<h3>* Setting a Config Item :-</h3>
&nbsp;
<ul>
<li>If you would like to dynamically set a config item or change an existing one, you can do so using:</li>
<li><strong>$this-&gt;config-&gt;set_item('<var>item_name</var>', '<var>item_value</var>');</strong></li>
<li>Where <var>item_name</var> is the $config array index you want to change, and <var>item_value</var> is its value.</li>
</ul>
&nbsp;
<h2>* Helper Functions :-</h2>
&nbsp;
<ul>
<li>The <strong>config class</strong> has the following helper functions:</li>
</ul>
&nbsp;
<h4>1) $this-&gt;config-&gt;site_url();</h4>
&nbsp;
<ul>
<li>This function retrieves the URL to your site, along with the "index" value you've specified in the config file.</li>
</ul>
&nbsp;
<h4>2) $this-&gt;config-&gt;base_url();</h4>
&nbsp;
<ul>
<li>This function retrieves the URL to your site, plus an optional path such as to a stylesheet or image.</li>
<li>The two functions above are normally accessed via the corresponding functions in the URL Helper.<a href="http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html">
</a></li>
</ul>
&nbsp;
<h4>3) $this-&gt;config-&gt;system_url();</h4>
&nbsp;
<div id="content">
<ul>
<li>This function retrieves the URL to your <dfn>system folder</dfn>.</li>
</ul>
</div>