blob: 71f77650c553831a53ba04e0c74aa2369e6f3aa2 [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 Allard8039d4c2008-05-31 02:47:56 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Derek Jonesfd93d222008-05-06 15:18:50 +00006<title>Loader Class : CodeIgniter User Guide</title>
adminb0dd10f2006-08-25 17:25:49 +00007
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
admin17a890d2006-09-27 20:42:42 +000011<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000012<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000013<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000014<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000015
adminb0dd10f2006-08-25 17:25:49 +000016<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 Jones1ca3fc42008-06-27 00:19:33 +000031<td><h1>CodeIgniter User Guide Version 1.6.3</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;
45Loader 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>Loader Class</h1>
60
admine334c472006-10-21 19:44:22 +000061<p>Loader, as the name suggests, is used to load elements. These elements can be libraries (classes) <a href="../general/views.html">View files</a>,
adminb0dd10f2006-08-25 17:25:49 +000062<a href="../general/helpers.html">Helpers</a>, <a href="../general/plugins.html">Plugins</a>, or your own 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<p>The following functions are available in this class:</p>
67
68
69
admin2f0bac82006-10-09 17:36:45 +000070<h2>$this->load->library('<var>class_name</var>')</h2>
71
admine334c472006-10-21 19:44:22 +000072<p>This function is used to load core classes. Where <var>class_name</var> is the name of the class you want to load.
admin2f0bac82006-10-09 17:36:45 +000073Note: We use the terms "class" and "library" interchangeably.</p>
74
Derek Allardd2df9bc2007-04-15 17:41:17 +000075<p>For example, if you would like to send email with CodeIgniter, the first step is to load the email class within your controller:</p>
admin2f0bac82006-10-09 17:36:45 +000076
77<code>$this->load->library('email');</code>
78
79<p>Once loaded, the library will be ready for use, using <kbd>$this->email-></kbd><samp><em>some_function</em>()</samp>.
80
Rick Ellis757ccf82008-08-12 01:00:30 +000081<p>Library files can be stored in subdirectories within the main "libraries" folder, or within your personal <dfn>application/libraries</dfn> folder.
82To load a file located in a subdirectory, simply include the path, relative to the "libraries" folder.
83For example, if you have file located at:</p>
admin2f0bac82006-10-09 17:36:45 +000084
Rick Ellis757ccf82008-08-12 01:00:30 +000085<code>libraries/flavors/chocolate.php</code>
86
87<p>You will load it using:</p>
88
89<code>$this->load->library('flavors/chocolate');</code>
90
91<p>You may nest the file in as many subdirectories as you want.</p>
92
93<p>Each library is described in detail in its own page, so please read the information regarding each one you would like to use.</p>
94
admincf493902006-10-10 07:12:31 +000095
96
adminb0dd10f2006-08-25 17:25:49 +000097<h2>$this->load->view('<var>file_name</var>', <samp>$data</samp>, <kbd>true/false</kbd>)</h2>
98
99<p>This function is used to load your View files. If you haven't read the <a href="../general/views.html">Views</a> section of the
100user guide it is recommended that you do since it shows you how this function is typically used.</p>
101
admin1b90c272006-10-31 18:22:29 +0000102<p>The first parameter is required. It is the name of the view file you would like to load. &nbsp;Note: The .php file extension does not need to be specified unless you use something other then <kbd>.php</kbd>.</p>
adminb0dd10f2006-08-25 17:25:49 +0000103
104<p>The second <strong>optional</strong> parameter can take
admine334c472006-10-21 19:44:22 +0000105an associative array or an object as input, which it runs through the PHP <a href="http://www.php.net/extract">extract</a> function to
adminb0dd10f2006-08-25 17:25:49 +0000106convert to variables that can be used in your view files. Again, read the <a href="../general/views.html">Views</a> page to learn
107how this might be useful.</p>
108
109<p>The third <strong>optional</strong> parameter lets you change the behavior of the function so that it returns data as a string
110rather than sending it to your browser. This can be useful if you want to process the data in some way. If you
111set the parameter to <kbd>true</kbd> (boolean) it will return data. The default behavior is <kbd>false</kbd>, which sends it
Derek Allardd8b92a32007-03-28 01:26:32 +0000112to your browser. Remember to assign it to a variable if you want the data returned:</p>
adminb0dd10f2006-08-25 17:25:49 +0000113
114<code>$string = $this->load->view('<var>myfile</var>', '', <kbd>true</kbd>);</code>
115
116
Derek Allardc8b21742008-05-18 03:57:40 +0000117<h2>$this-&gt;load-&gt;model('<var>Model_name</var>');</h2>
118<p><code>$this-&gt;load-&gt;model('<var>Model_name</var>');</code></p>
Derek Allard6d504e12008-05-26 02:34:43 +0000119<p>If your model is located in a sub-folder, include the relative path from your models folder. For example, if you have a model located at application/models/blog/queries.php you'll load it using:</p>
Derek Allardc8b21742008-05-18 03:57:40 +0000120<p><code>$this-&gt;load-&gt;model('<var>blog/queries</var>');</code></p>
121<p>If you would like your model assigned to a different object name you can specify it via the second parameter of the loading
122 function:</p>
123<code> $this-&gt;load-&gt;model('<var>Model_name</var>', '<kbd>fubar</kbd>');<br />
124<br />
125$this-&gt;<kbd>fubar</kbd>-&gt;function();</code>
adminb0dd10f2006-08-25 17:25:49 +0000126<h2>$this->load->database('<var>options</var>', <kbd>true/false</kbd>)</h2>
admine334c472006-10-21 19:44:22 +0000127<p>This function lets you load the database class. The two parameters are <strong>optional</strong>. Please see the
adminb0dd10f2006-08-25 17:25:49 +0000128<a href="./database/index.html">database</a> section for more info.</p>
129
130
131<h2>$this->load->scaffolding('<var>table_name</var>')</h2>
132
admine334c472006-10-21 19:44:22 +0000133<p>This function lets you enable scaffolding. Please see the
adminb0dd10f2006-08-25 17:25:49 +0000134<a href="../general/scaffolding.html">scaffolding</a> section for more info.</p>
135
136
137
138<h2>$this->load->vars(<samp>$array</samp>)</h2>
139
140<p>This function takes an associative array as input and generates variables using the PHP <a href="http://www.php.net/extract">extract</a> function.
admine334c472006-10-21 19:44:22 +0000141This function produces the same result as using the second parameter of the <dfn>$this->load->view()</dfn> function above. The reason you might
142want to use this function independently is if you would like to set some global variables in the constructor of your controller
143and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached
adminb0dd10f2006-08-25 17:25:49 +0000144and merged into one array for conversion to variables.
145</p>
146
147
148<h2>$this->load->helper('<var>file_name</var>')</h2>
149<p>This function loads helper files, where <var>file_name</var> is the name of the file, without the <kbd>_helper.php</kbd> extension.</p>
150
151
152<h2>$this->load->plugin('<var>file_name</var>')</h2>
153<p>This function loads plugins files, where <var>file_name</var> is the name of the file, without the <kbd>_plugin.php</kbd> extension.</p>
154
155<h2>$this->load->file('<var>filepath/filename</var>', <kbd>true/false</kbd>)</h2>
admine334c472006-10-21 19:44:22 +0000156<p>This is a generic file loading function. Supply the filepath and name in the first parameter and it will open and read the file.
adminb0dd10f2006-08-25 17:25:49 +0000157By default the data is sent to your browser, just like a View file, but if you set the second parameter to <kbd>true</kbd> (boolean)
158it will instead return the data as a string.</p>
159
160
Derek Jonesfd93d222008-05-06 15:18:50 +0000161<h2>$this->load->lang('<var>file_name</var>')</h2>
adminb0dd10f2006-08-25 17:25:49 +0000162<p>This function is an alias of the <a href="language.html">language loading function</a>: $this->lang->load()</p>
163
164<h2>$this->load->config('<var>file_name</var>')</h2>
165<p>This function is an alias of the <a href="config.html">config file loading function</a>: $this->config->load()</p>
166
167
168
169
170</div>
171<!-- END CONTENT -->
172
173
174<div id="footer">
175<p>
176Previous Topic:&nbsp;&nbsp;<a href="input.html">Input Class</a>
177&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
178<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
179<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
180Next Topic:&nbsp;&nbsp;<a href="language.html">Language Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000181</p>
Derek Jones07870432008-02-13 03:49:26 +0000182<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000183</div>
184
185</body>
186</html>