blob: af27176addea290f30ebe66743707a861a36672e [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!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>Loader 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 Jonesb8c038a2011-08-20 08:57:14 -050031<td><h1>CodeIgniter User Guide Version 2.0.3</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000032<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> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Loader 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&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>
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>Loader Class</h1>
60
Derek Jones4b9c6292011-07-01 17:40:48 -050061<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>,
Derek Jonesc6da5032010-03-09 20:44:27 -060062<a href="../general/helpers.html">Helpers</a>, <a href="../general/models.html">Models</a>, or your own files.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000063
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<h2>$this->load->library('<var>class_name</var>', <samp>$config</samp>, <kbd>'object name'</kbd>)</h2>
70
71
Derek Jones4b9c6292011-07-01 17:40:48 -050072<p>This function is used to load core classes. Where <var>class_name</var> is the name of the class you want to load.
Derek Allard2067d1a2008-11-13 22:59:24 +000073Note: We use the terms "class" and "library" interchangeably.</p>
74
75<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>
76
77<code>$this->load->library('email');</code>
78
Derek Allard8d027fa2010-07-05 07:47:38 -040079<p>Once loaded, the library will be ready for use, using <kbd>$this->email-></kbd><samp><em>some_function</em>()</samp>.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +000080
Barry Mienydd671972010-10-04 16:33:58 +020081<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.
Derek Allard2067d1a2008-11-13 22:59:24 +000083For example, if you have file located at:</p>
84
85<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
Kellas Reeves3c6e4852011-02-09 11:57:56 -060093<p>Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load function.</p>
94
95<code>$this->load->library(array('<var>email</var>', '<var>table</var>'));</code>
96
Derek Allard2067d1a2008-11-13 22:59:24 +000097<h3>Setting options</h3>
98
Derek Jones4b9c6292011-07-01 17:40:48 -050099<p>The second (optional) parameter allows you to optionally pass configuration setting. You will typically pass these as an array:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000100
101<code>
102$config = array (<br />
103&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'mailtype' => 'html',<br />
104&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'charset'&nbsp; => 'utf-8,<br />
105&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'priority' => '1'<br />
106&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
107<br />
108$this->load->library('email', $config);</code>
109
110<p>Config options can usually also be set via a config file. Each library is explained in detail in its own page, so please read the information regarding each one you would like to use.</p>
111
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600112<p>Please take note, when multiple libraries are supplied in an array for the first parameter, each will receive the same parameter information.</p>
113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114<h3>Assigning a Library to a different object name</h3>
115
Derek Jones4b9c6292011-07-01 17:40:48 -0500116<p>If the third (optional) parameter is blank, the library will usually be assigned to an object with the same name as the library. For example, if the library is named <dfn>Session</dfn>, it
Derek Allard2067d1a2008-11-13 22:59:24 +0000117will be assigned to a variable named <dfn>$this->session</dfn>.</p>
118
119<p>If you prefer to set your own class names you can pass its value to the third parameter:</p>
120
121<code>$this->load->library('session', '', 'my_session');<br /><br />
122
123// Session class is now accessed using:<br /><br />
124
125$this->my_session
126
127</code>
128
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600129<p>Please take note, when multiple libraries are supplied in an array for the first parameter, this parameter is discarded.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000130
131
132<h2>$this->load->view('<var>file_name</var>', <samp>$data</samp>, <kbd>true/false</kbd>)</h2>
133
Derek Jones4b9c6292011-07-01 17:40:48 -0500134<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
Derek Allard2067d1a2008-11-13 22:59:24 +0000135user guide it is recommended that you do since it shows you how this function is typically used.</p>
136
Derek Jones4b9c6292011-07-01 17:40:48 -0500137<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 than <kbd>.php</kbd>.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000138
139<p>The second <strong>optional</strong> parameter can take
140an associative array or an object as input, which it runs through the PHP <a href="http://www.php.net/extract">extract</a> function to
Derek Jones4b9c6292011-07-01 17:40:48 -0500141convert to variables that can be used in your view files. Again, read the <a href="../general/views.html">Views</a> page to learn
Derek Allard2067d1a2008-11-13 22:59:24 +0000142how this might be useful.</p>
143
144<p>The third <strong>optional</strong> parameter lets you change the behavior of the function so that it returns data as a string
Derek Jones4b9c6292011-07-01 17:40:48 -0500145rather than sending it to your browser. This can be useful if you want to process the data in some way. If you
146set the parameter to <kbd>true</kbd> (boolean) it will return data. The default behavior is <kbd>false</kbd>, which sends it
147to your browser. Remember to assign it to a variable if you want the data returned:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000148
149<code>$string = $this->load->view('<var>myfile</var>', '', <kbd>true</kbd>);</code>
150
151
152<h2>$this-&gt;load-&gt;model('<var>Model_name</var>');</h2>
153<p><code>$this-&gt;load-&gt;model('<var>Model_name</var>');</code></p>
154<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>
155<p><code>$this-&gt;load-&gt;model('<var>blog/queries</var>');</code></p>
156<p>If you would like your model assigned to a different object name you can specify it via the second parameter of the loading
157 function:</p>
158<code> $this-&gt;load-&gt;model('<var>Model_name</var>', '<kbd>fubar</kbd>');<br />
159<br />
160$this-&gt;<kbd>fubar</kbd>-&gt;function();</code>
161<h2>$this->load->database('<var>options</var>', <kbd>true/false</kbd>)</h2>
Derek Jones4b9c6292011-07-01 17:40:48 -0500162<p>This function lets you load the database class. The two parameters are <strong>optional</strong>. Please see the
Derek Allard3f45bbd2008-12-07 17:00:09 +0000163<a href="../database/index.html">database</a> section for more info.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000164
165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166
167
168<h2>$this->load->vars(<samp>$array</samp>)</h2>
169
170<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.
Derek Jones4b9c6292011-07-01 17:40:48 -0500171This function produces the same result as using the second parameter of the <dfn>$this->load->view()</dfn> function above. The reason you might
Derek Allard2067d1a2008-11-13 22:59:24 +0000172want to use this function independently is if you would like to set some global variables in the constructor of your controller
Derek Jones4b9c6292011-07-01 17:40:48 -0500173and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached
Derek Allard2067d1a2008-11-13 22:59:24 +0000174and merged into one array for conversion to variables.
175</p>
176
177
Phil Sturgeone0df0792011-07-22 16:11:34 -0600178<h2>$this->load->get_var(<samp>$key</samp>)</h2>
179
180<p>This function checks the associative array of variables available to your views. This is useful if for any reason a var is set in a library or another controller method using $this->load->vars().
181</p>
182
183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184<h2>$this->load->helper('<var>file_name</var>')</h2>
185<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>
186
187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188<h2>$this->load->file('<var>filepath/filename</var>', <kbd>true/false</kbd>)</h2>
Derek Jones4b9c6292011-07-01 17:40:48 -0500189<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.
Derek Allard2067d1a2008-11-13 22:59:24 +0000190By 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)
191it will instead return the data as a string.</p>
192
193
Derek Allardb5f211b2010-01-17 15:42:56 +0000194<h2>$this->load->language('<var>file_name</var>')</h2>
Derek Allard2067d1a2008-11-13 22:59:24 +0000195<p>This function is an alias of the <a href="language.html">language loading function</a>: $this->lang->load()</p>
196
197<h2>$this->load->config('<var>file_name</var>')</h2>
198<p>This function is an alias of the <a href="config.html">config file loading function</a>: $this->config->load()</p>
199
200
Derek Jones2ede2f62010-03-10 15:00:18 -0600201<h2>Application "Packages"</h2>
202
Derek Jones4b9c6292011-07-01 17:40:48 -0500203<p>An application package allows for the easy distribution of complete sets of resources in a single directory, complete with its own libraries, models, helpers, config, and language files. It is recommended that these packages be placed in the <dfn>application/third_party</dfn> folder. Below is a sample map of an package directory</p>
Derek Jones2ede2f62010-03-10 15:00:18 -0600204
205
206<h2>Sample Package "Foo Bar" Directory Map</h2>
207
208<p>The following is an example of a directory for an application package named "Foo Bar".</p>
209
Derek Jonesf0b39942010-03-25 10:08:20 -0500210<code>/application/third_party/foo_bar<br />
Derek Jones2ede2f62010-03-10 15:00:18 -0600211<br />
212config/<br />
213helpers/<br />
214language/<br />
215libraries/<br />
216models/<br />
217</code>
218
Derek Jones4b9c6292011-07-01 17:40:48 -0500219<p>Whatever the purpose of the "Foo Bar" application package, it has its own config files, helpers, language files, libraries, and models. To use these resources in your controllers, you first need to tell the Loader that you are going to be loading resources from a package, by adding the package path.</p>
Derek Jones2ede2f62010-03-10 15:00:18 -0600220
221<h3>$this->load->add_package_path()</h3>
222
Derek Jones4b9c6292011-07-01 17:40:48 -0500223<p>Adding a package path instructs the Loader class to prepend a given path for subsequent requests for resources. As an example, the "Foo Bar" application package above has a library named <dfn>Foo_bar.php</dfn>. In our controller, we'd do the following:</p>
Derek Jones2ede2f62010-03-10 15:00:18 -0600224
225<code>$this->load->add_package_path(<var>APPPATH</var>.'third_party/<var>foo_bar</var>/');<br />
226$this->load->library('foo_bar');</code>
227
228<h3>$this->load->remove_package_path()</h3>
229
Derek Jones4b9c6292011-07-01 17:40:48 -0500230<p>When your controller is finished using resources from an application package, and particularly if you have other application packages you want to work with, you may wish to remove the package path so the Loader no longer looks in that folder for resources. To remove the last path added, simply call the method with no parameters.</p>
Derek Jones2ede2f62010-03-10 15:00:18 -0600231
232<h3>$this->load->remove_package_path()</h3>
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Jones2ede2f62010-03-10 15:00:18 -0600234<p>Or to remove a specific package path, specify the same path previously given to <kbd>add_package_path() for a package.</kbd>:</p>
235
236<code>$this->load->remove_package_path(<var>APPPATH</var>.'third_party/<var>foo_bar</var>/');</code>
237
238<h3>Package view files</h3>
239
Derek Jones4b9c6292011-07-01 17:40:48 -0500240<p>By Default, package view files paths are set when <samp>add_package_path()</samp> is called. View paths are looped through, and once a match is encountered that view is loaded.</p>
241<p>In this instance, it is possible for view naming collisions within packages to occur, and possibly the incorrect package being loaded. To ensure against this, set an optional second parameter of <var>FALSE</var> when calling <samp>add_package_path()</samp>.</p>
Derek Jones2ede2f62010-03-10 15:00:18 -0600242
Greg Akere08c5272011-04-20 09:54:47 -0500243<code>
244$this->load->add_package_path(APPPATH.'my_app', TRUE);<br>
245$this->load->view('my_app_index'); // Loads<br>
246$this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is TRUE<br>
247<br>
248// Reset things<br>
249$this->load->remove_package_path(APPPATH.'my_app');<br>
250<br>
251// Again without the second parameter:<br>
252$this->load->add_package_path(APPPATH.'my_app', TRUE);<br>
253$this->load->view('my_app_index'); // Loads<br>
254$this->load->view('welcome_message'); // Loads<br>
255</code>
Derek Allard2067d1a2008-11-13 22:59:24 +0000256
257</div>
258<!-- END CONTENT -->
259
260
261<div id="footer">
262<p>
263Previous Topic:&nbsp;&nbsp;<a href="input.html">Input Class</a>
264&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
265<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
266<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
267Next Topic:&nbsp;&nbsp;<a href="language.html">Language Class</a>
268</p>
Derek Jones898949f2011-01-28 07:42:16 -0600269<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000270</div>
271
272</body>
adminb0dd10f2006-08-25 17:25:49 +0000273</html>