blob: bbe2ed53076fcf8621e669b6f4e93ff46300b1ff [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001############
2Loader Class
3############
4
5Loader, as the name suggests, is used to load elements. These elements
6can be libraries (classes) :doc:`View files <../general/views>`,
7:doc:`Helpers <../general/helpers>`,
8:doc:`Models <../general/models>`, or your own files.
9
10.. note:: This class is initialized automatically by the system so there
11 is no need to do it manually.
12
13The following functions are available in this class:
14
15$this->load->library('class_name', $config, 'object name')
16===========================================================
17
18This function is used to load core classes. Where class_name is the
19name of the class you want to load. Note: We use the terms "class" and
20"library" interchangeably.
21
22For example, if you would like to send email with CodeIgniter, the first
23step is to load the email class within your controller::
24
25 $this->load->library('email');
26
27Once loaded, the library will be ready for use, using
28$this->email->*some_function*().
29
30Library files can be stored in subdirectories within the main
31"libraries" folder, or within your personal application/libraries
32folder. To load a file located in a subdirectory, simply include the
33path, relative to the "libraries" folder. For example, if you have file
34located at::
35
36 libraries/flavors/chocolate.php
37
38You will load it using::
39
40 $this->load->library('flavors/chocolate');
41
42You may nest the file in as many subdirectories as you want.
43
44Additionally, multiple libraries can be loaded at the same time by
45passing an array of libraries to the load function.
46
47::
48
49 $this->load->library(array('email', 'table'));
50
51Setting options
52---------------
53
54The second (optional) parameter allows you to optionally pass
55configuration setting. You will typically pass these as an array::
56
Derek Jones36be9692011-10-05 15:52:41 -050057 $config = array (
58 'mailtype' => 'html',
59 'charset' => 'utf-8,
60 'priority' => '1'
61 );
62
63 $this->load->library('email', $config);
Derek Jones8ede1a22011-10-05 13:34:52 -050064
65Config options can usually also be set via a config file. Each library
66is explained in detail in its own page, so please read the information
67regarding each one you would like to use.
68
69Please take note, when multiple libraries are supplied in an array for
70the first parameter, each will receive the same parameter information.
71
72Assigning a Library to a different object name
73----------------------------------------------
74
75If the third (optional) parameter is blank, the library will usually be
76assigned to an object with the same name as the library. For example, if
77the library is named Session, it will be assigned to a variable named
78$this->session.
79
80If you prefer to set your own class names you can pass its value to the
81third parameter::
82
Derek Jones36be9692011-10-05 15:52:41 -050083 $this->load->library('session', '', 'my_session');
84
85 // Session class is now accessed using:
86
87 $this->my_session
Derek Jones8ede1a22011-10-05 13:34:52 -050088
89Please take note, when multiple libraries are supplied in an array for
90the first parameter, this parameter is discarded.
91
92$this->load->view('file_name', $data, true/false)
93==================================================
94
95This function is used to load your View files. If you haven't read the
96:doc:`Views <../general/views>` section of the user guide it is
97recommended that you do since it shows you how this function is
98typically used.
99
100The first parameter is required. It is the name of the view file you
101would like to load. Note: The .php file extension does not need to be
102specified unless you use something other than .php.
103
104The second **optional** parameter can take an associative array or an
105object as input, which it runs through the PHP
106`extract <http://www.php.net/extract>`_ function to convert to variables
107that can be used in your view files. Again, read the
108:doc:`Views <../general/views>` page to learn how this might be useful.
109
110The third **optional** parameter lets you change the behavior of the
111function so that it returns data as a string rather than sending it to
112your browser. This can be useful if you want to process the data in some
113way. If you set the parameter to true (boolean) it will return data. The
114default behavior is false, which sends it to your browser. Remember to
115assign it to a variable if you want the data returned::
116
117 $string = $this->load->view('myfile', '', true);
118
119$this->load->model('Model_name');
120==================================
121
122::
123
124 $this->load->model('Model_name');
125
126
127If your model is located in a sub-folder, include the relative path from
128your models folder. For example, if you have a model located at
129application/models/blog/queries.php you'll load it using::
130
131 $this->load->model('blog/queries');
132
133
134If you would like your model assigned to a different object name you can
135specify it via the second parameter of the loading function::
136
Derek Jones36be9692011-10-05 15:52:41 -0500137 $this->load->model('Model_name', 'fubar');
138
139 $this->fubar->function();
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
141$this->load->database('options', true/false)
142============================================
143
144This function lets you load the database class. The two parameters are
145**optional**. Please see the :doc:`database <../database/index>`
146section for more info.
147
148$this->load->vars($array)
149=========================
150
151This function takes an associative array as input and generates
152variables using the PHP `extract <http://www.php.net/extract>`_
153function. This function produces the same result as using the second
154parameter of the $this->load->view() function above. The reason you
155might want to use this function independently is if you would like to
156set some global variables in the constructor of your controller and have
157them become available in any view file loaded from any function. You can
158have multiple calls to this function. The data get cached and merged
159into one array for conversion to variables.
160
161$this->load->get_var($key)
162===========================
163
164This function checks the associative array of variables available to
165your views. This is useful if for any reason a var is set in a library
166or another controller method using $this->load->vars().
167
168$this->load->helper('file_name')
169=================================
170
171This function loads helper files, where file_name is the name of the
172file, without the _helper.php extension.
173
174$this->load->file('filepath/filename', true/false)
175==================================================
176
177This is a generic file loading function. Supply the filepath and name in
178the first parameter and it will open and read the file. By default the
179data is sent to your browser, just like a View file, but if you set the
180second parameter to true (boolean) it will instead return the data as a
181string.
182
183$this->load->language('file_name')
184===================================
185
186This function is an alias of the :doc:`language loading
187function <language>`: $this->lang->load()
188
189$this->load->config('file_name')
190=================================
191
192This function is an alias of the :doc:`config file loading
193function <config>`: $this->config->load()
194
195Application "Packages"
196======================
197
198An application package allows for the easy distribution of complete sets
199of resources in a single directory, complete with its own libraries,
200models, helpers, config, and language files. It is recommended that
201these packages be placed in the application/third_party folder. Below
202is a sample map of an package directory
203
204Sample Package "Foo Bar" Directory Map
205======================================
206
207The following is an example of a directory for an application package
208named "Foo Bar".
209
210::
211
Derek Jones36be9692011-10-05 15:52:41 -0500212 /application/third_party/foo_bar
213
214 config/
215 helpers/
216 language/
217 libraries/
218 models/
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
220Whatever the purpose of the "Foo Bar" application package, it has its
221own config files, helpers, language files, libraries, and models. To use
222these resources in your controllers, you first need to tell the Loader
223that you are going to be loading resources from a package, by adding the
224package path.
225
226$this->load->add_package_path()
227---------------------------------
228
229Adding a package path instructs the Loader class to prepend a given path
230for subsequent requests for resources. As an example, the "Foo Bar"
231application package above has a library named Foo_bar.php. In our
232controller, we'd do the following::
233
Derek Jones36be9692011-10-05 15:52:41 -0500234 $this->load->add_package_path(APPPATH.'third_party/foo_bar/');
235 $this->load->library('foo_bar');
Derek Jones8ede1a22011-10-05 13:34:52 -0500236
237$this->load->remove_package_path()
238------------------------------------
239
240When your controller is finished using resources from an application
241package, and particularly if you have other application packages you
242want to work with, you may wish to remove the package path so the Loader
243no longer looks in that folder for resources. To remove the last path
244added, simply call the method with no parameters.
245
246$this->load->remove_package_path()
247------------------------------------
248
249Or to remove a specific package path, specify the same path previously
250given to add_package_path() for a package.::
251
252 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
253
254Package view files
255------------------
256
257By Default, package view files paths are set when add_package_path()
258is called. View paths are looped through, and once a match is
259encountered that view is loaded.
260
261In this instance, it is possible for view naming collisions within
262packages to occur, and possibly the incorrect package being loaded. To
263ensure against this, set an optional second parameter of FALSE when
264calling add_package_path().
265
266::
267
268 $this->load->add_package_path(APPPATH.'my_app', FALSE);
269 $this->load->view('my_app_index'); // Loads
270 $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE
271
272 // Reset things
273 $this->load->remove_package_path(APPPATH.'my_app');
274
275 // Again without the second parameter:
276 $this->load->add_package_path(APPPATH.'my_app', TRUE);
277 $this->load->view('my_app_index'); // Loads
278 $this->load->view('welcome_message'); // Loads