blob: 20e1c1d3c1b52e5c54ce40ad6f67f8d83ff5214a [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>`,
dchill423169f262012-08-11 20:12:05 -04007:doc:`Drivers <../general/drivers>`,
Derek Jones8ede1a22011-10-05 13:34:52 -05008:doc:`Helpers <../general/helpers>`,
9:doc:`Models <../general/models>`, or your own files.
10
11.. note:: This class is initialized automatically by the system so there
12 is no need to do it manually.
13
Andrey Andreev05563962014-01-06 12:28:59 +020014.. contents::
15 :local:
Derek Jones8ede1a22011-10-05 13:34:52 -050016
Andrey Andreev05563962014-01-06 12:28:59 +020017.. raw:: html
Derek Jones8ede1a22011-10-05 13:34:52 -050018
Andrey Andreev05563962014-01-06 12:28:59 +020019 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050020
21Application "Packages"
22======================
23
24An application package allows for the easy distribution of complete sets
25of resources in a single directory, complete with its own libraries,
26models, helpers, config, and language files. It is recommended that
Andrey Andreevc26d34f2013-01-28 21:46:08 +020027these packages be placed in the application/third_party directory. Below
Andrey Andreev05563962014-01-06 12:28:59 +020028is a sample map of an package directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050029
30The following is an example of a directory for an application package
31named "Foo Bar".
32
33::
34
Derek Jones36be9692011-10-05 15:52:41 -050035 /application/third_party/foo_bar
36
37 config/
38 helpers/
39 language/
40 libraries/
41 models/
Derek Jones8ede1a22011-10-05 13:34:52 -050042
43Whatever the purpose of the "Foo Bar" application package, it has its
44own config files, helpers, language files, libraries, and models. To use
45these resources in your controllers, you first need to tell the Loader
46that you are going to be loading resources from a package, by adding the
Andrey Andreev05563962014-01-06 12:28:59 +020047package path via the ``add_package_path()`` method.
Derek Jones8ede1a22011-10-05 13:34:52 -050048
49Package view files
50------------------
51
Andrey Andreev05563962014-01-06 12:28:59 +020052By Default, package view files paths are set when ``add_package_path()``
Derek Jones8ede1a22011-10-05 13:34:52 -050053is called. View paths are looped through, and once a match is
54encountered that view is loaded.
55
56In this instance, it is possible for view naming collisions within
57packages to occur, and possibly the incorrect package being loaded. To
58ensure against this, set an optional second parameter of FALSE when
Andrey Andreev05563962014-01-06 12:28:59 +020059calling ``add_package_path()``.
Derek Jones8ede1a22011-10-05 13:34:52 -050060
61::
62
63 $this->load->add_package_path(APPPATH.'my_app', FALSE);
64 $this->load->view('my_app_index'); // Loads
65 $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE
66
67 // Reset things
68 $this->load->remove_package_path(APPPATH.'my_app');
69
70 // Again without the second parameter:
W. Kristiantoa5e329f2012-09-23 22:34:18 +070071 $this->load->add_package_path(APPPATH.'my_app');
Derek Jones8ede1a22011-10-05 13:34:52 -050072 $this->load->view('my_app_index'); // Loads
Andrey Andreev05563962014-01-06 12:28:59 +020073 $this->load->view('welcome_message'); // Loads
74
75***************
76Class Reference
77***************
78
79.. class:: CI_Loader
80
81 .. method:: library($library[, $params = NULL[, $object_name = NULL]])
82
83 :param mixed $library: Library name as a string or an array with multiple libraries
84 :param array $params: Optional array of parameters to pass to the loaded library's constructor
85 :param string $object_name: Optional object name to assign the library to
86 :returns: void
87
88 This method is used to load core classes.
89
90 .. note:: We use the terms "class" and "library" interchangeably.
91
92 For example, if you would like to send email with CodeIgniter, the first
93 step is to load the email class within your controller::
94
95 $this->load->library('email');
96
97 Once loaded, the library will be ready for use, using ``$this->email``.
98
99 Library files can be stored in subdirectories within the main
100 "libraries" directory, or within your personal *application/libraries*
101 directory. To load a file located in a subdirectory, simply include the
102 path, relative to the "libraries" directory. For example, if you have
103 file located at::
104
105 libraries/flavors/Chocolate.php
106
107 You will load it using::
108
109 $this->load->library('flavors/chocolate');
110
111 You may nest the file in as many subdirectories as you want.
112
113 Additionally, multiple libraries can be loaded at the same time by
114 passing an array of libraries to the load method.
115 ::
116
117 $this->load->library(array('email', 'table'));
118
119 Setting options
120 ---------------
121
122 The second (optional) parameter allows you to optionally pass
123 configuration setting. You will typically pass these as an array::
124
125 $config = array (
126 'mailtype' => 'html',
127 'charset' => 'utf-8,
128 'priority' => '1'
129 );
130
131 $this->load->library('email', $config);
132
133 Config options can usually also be set via a config file. Each library
134 is explained in detail in its own page, so please read the information
135 regarding each one you would like to use.
136
137 Please take note, when multiple libraries are supplied in an array for
138 the first parameter, each will receive the same parameter information.
139
140 Assigning a Library to a different object name
141 ----------------------------------------------
142
143 If the third (optional) parameter is blank, the library will usually be
144 assigned to an object with the same name as the library. For example, if
145 the library is named Calendar, it will be assigned to a variable named
146 ``$this->calendar``.
147
148 If you prefer to set your own class names you can pass its value to the
149 third parameter::
150
151 $this->load->library('calendar', NULL, 'my_calendar');
152
153 // Calendar class is now accessed using:
154 $this->my_calendar
155
156 Please take note, when multiple libraries are supplied in an array for
157 the first parameter, this parameter is discarded.
158
159 .. method:: driver($library[, $params = NULL[, $object_name]])
160
161 :param mixed $library: Library name as a string or an array with multiple libraries
162 :param array $params: Optional array of parameters to pass to the loaded library's constructor
163 :param string $object_name: Optional object name to assign the library to
164 :returns: void
165
166 This method is used to load driver libraries, acts very much like the
167 ``library()`` method.
168
169 As an example, if you would like to use sessions with CodeIgniter, the first
170 step is to load the session driver within your controller::
171
172 $this->load->driver('session');
173
174 Once loaded, the library will be ready for use, using ``$this->session``.
175
176 Driver files must be stored in a subdirectory within the main
177 "libraries" directory, or within your personal *application/libraries*
178 directory. The subdirectory must match the parent class name. Read the
179 :doc:`Drivers <../general/drivers>` description for details.
180
181 Additionally, multiple driver libraries can be loaded at the same time by
182 passing an array of drivers to the load method.
183 ::
184
185 $this->load->driver(array('session', 'cache'));
186
187 Setting options
188 ---------------
189
190 The second (optional) parameter allows you to optionally pass
191 configuration settings. You will typically pass these as an array::
192
193 $config = array(
194 'sess_driver' => 'cookie',
195 'sess_encrypt_cookie' => true,
196 'encryption_key' => 'mysecretkey'
197 );
198
199 $this->load->driver('session', $config);
200
201 Config options can usually also be set via a config file. Each library
202 is explained in detail in its own page, so please read the information
203 regarding each one you would like to use.
204
205 Assigning a Driver to a different object name
206 ---------------------------------------------
207
208 If the third (optional) parameter is blank, the library will be assigned
209 to an object with the same name as the parent class. For example, if
210 the library is named Session, it will be assigned to a variable named
211 ``$this->session``.
212
213 If you prefer to set your own class names you can pass its value to the
214 third parameter::
215
216 $this->load->library('session', '', 'my_session');
217
218 // Session class is now accessed using:
219 $this->my_session
220
221 .. method:: view($view[, $vars = array()[, return = FALSE]])
222
223 :param string $view: View name
224 :param array $vars: An associative array of variables
225 :param bool $return: Whether to return the loaded view
226 :returns: mixed
227
228 This method is used to load your View files. If you haven't read the
229 :doc:`Views <../general/views>` section of the user guide it is
230 recommended that you do since it shows you how this method is
231 typically used.
232
233 The first parameter is required. It is the name of the view file you
234 would like to load.
235
236 .. note:: The .php file extension does not need to be specified unless
237 you use something other than .php.
238
239 The second **optional** parameter can take an associative array or an
240 object as input, which it runs through the PHP
241 `extract() <http://www.php.net/extract>`_ function to convert to variables
242 that can be used in your view files. Again, read the
243 :doc:`Views <../general/views>` page to learn how this might be useful.
244
245 The third **optional** parameter lets you change the behavior of the
246 method so that it returns data as a string rather than sending it to
247 your browser. This can be useful if you want to process the data in some
248 way. If you set the parameter to TRUE (boolean) it will return data. The
249 default behavior is FALSE, which sends it to your browser. Remember to
250 assign it to a variable if you want the data returned::
251
252 $string = $this->load->view('myfile', '', TRUE);
253
254 .. method:: vars($vars[, $val = ''])
255
256 :param mixed $vars: An array of variables or a single variable name
257 :param mixed $val: Optional variable value
258 :returns: void
259
260 This method takes an associative array as input and generates
261 variables using the PHP `extract() <http://www.php.net/extract>`_
262 function. This method produces the same result as using the second
263 parameter of the ``$this->load->view()`` method above. The reason you
264 might want to use this method independently is if you would like to
265 set some global variables in the constructor of your controller and have
266 them become available in any view file loaded from any method. You can
267 have multiple calls to this method. The data get cached and merged
268 into one array for conversion to variables.
269
270 .. method:: get_var($key)
271
272 :param string $key: Variable name key
273 :returns: mixed
274
275 This method checks the associative array of variables available to
276 your views. This is useful if for any reason a var is set in a library
277 or another controller method using ``$this->load->vars()``.
278
279 .. method:: get_vars()
280
281 :returns: array
282
283 This method retrieves all variables available to your views.
284
285 .. method:: model($model[, $name = ''[, $db_conn = FALSE]])
286
287 :param mixed $model: Model name or an array containing multiple models
288 :param string $name: Optional object name to assign the model to
289 :param string $db_conn: Optional database configuration group to load
290 :returns: void
291
292 ::
293
294 $this->load->model('model_name');
295
296
297 If your model is located in a subdirectory, include the relative path
298 from your models directory. For example, if you have a model located at
299 *application/models/blog/queries.php* you'll load it using::
300
301 $this->load->model('blog/queries');
302
303 If you would like your model assigned to a different object name you can
304 specify it via the second parameter of the loading method::
305
306 $this->load->model('model_name', 'fubar');
307 $this->fubar->method();
308
309 .. method:: database([$params = ''[, $return = FALSE[, $query_builder = NULL]]])
310
311 :param mixed $params: Database group name or configuration options
312 :param bool $return: Whether to return the loaded database object
313 :param bool $query_builder: Whether to load the Query Builder
314 :returns: mixed
315
316 This method lets you load the database class. The two parameters are
317 **optional**. Please see the :doc:`database <../database/index>`
318 section for more info.
319
320 .. method:: dbforge([$db = NULL[, $return = FALSE]])
321
322 :param object $db: Database object
323 :param bool $return: Whether to return the Database Forge instance
324 :returns: mixed
325
326 Loads the :doc:`Database Forge <../database/forge>` class, please refer
327 to that manual for more info.
328
329 .. method:: dbutil([$db = NULL[, $return = FALSE]])
330
331 :param object $db: Database object
332 :param bool $return: Whether to return the Database Utilities instance
333 :returns: mixed
334
335 Loads the :doc:`Database Utilities <../database/utilities>` class, please
336 refer to that manual for more info.
337
338 .. method:: helper($helpers)
339
340 :param mixed $helpers: Helper name as a string or an array containing multiple helpers
341 :returns: void
342
343 This method loads helper files, where file_name is the name of the
344 file, without the _helper.php extension.
345
346 .. method:: file($path[, $return = FALSE])
347
348 :param string $path: File path
349 :param bool $return: Whether to return the loaded file
350 :returns: mixed
351
352 This is a generic file loading method. Supply the filepath and name in
353 the first parameter and it will open and read the file. By default the
354 data is sent to your browser, just like a View file, but if you set the
355 second parameter to boolean TRUE it will instead return the data as a
356 string.
357
358 .. method:: language($files[, $lang = ''])
359
360 :param mixed $files: Language file name or an array of multiple language files
361 :param string $lang: Language name
362 :returns: void
363
364 This method is an alias of the :doc:`language loading
365 method <language>`: ``$this->lang->load()``.
366
367 .. method:: config($file[, $use_sections = FALSE[, $fail_gracefully = FALSE]])
368
369 :param string $file: Configuration file name
370 :param bool $use_sections: Whether configuration values should be loaded into their own section
371 :param bool $fail_gracefully: Whether to just return FALSE in case of failure
372 :returns: bool
373
374 This method is an alias of the :doc:`config file loading
375 method <config>`: ``$this->config->load()``
376
377 .. method:: add_package_path($path[, $view_cascade = TRUE])
378
379 :param string $path: Path to add
380 :param bool $view_cascade: Whether to use cascading views
381 :returns: void
382
383 Adding a package path instructs the Loader class to prepend a given path
384 for subsequent requests for resources. As an example, the "Foo Bar"
385 application package above has a library named Foo_bar.php. In our
386 controller, we'd do the following::
387
388 $this->load->add_package_path(APPPATH.'third_party/foo_bar/');
389 $this->load->library('foo_bar');
390
391 .. method:: remove_package_path([$path = ''])
392
393 :param string $path: Path to remove
394 :returns: void
395
396 When your controller is finished using resources from an application
397 package, and particularly if you have other application packages you
398 want to work with, you may wish to remove the package path so the Loader
399 no longer looks in that directory for resources. To remove the last path
400 added, simply call the method with no parameters.
401
402 Or to remove a specific package path, specify the same path previously
403 given to ``add_package_path()`` for a package.::
404
405 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
406
407 .. method:: get_package_paths([$include_base = TRUE])
408
409 :param bool $include_base: Whether to include BASEPATH
410 :returns: array
411
412 Returns all currently available package paths.