blob: 107b3ece31a92dc2ce7ac347c181abc7030dbb7c [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
Andrey Andreev28c2c972014-02-08 04:27:48 +020083 :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: CI_Loader instance (method chaining)
87 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +020088
89 This method is used to load core classes.
90
91 .. note:: We use the terms "class" and "library" interchangeably.
92
93 For example, if you would like to send email with CodeIgniter, the first
94 step is to load the email class within your controller::
95
96 $this->load->library('email');
97
98 Once loaded, the library will be ready for use, using ``$this->email``.
99
100 Library files can be stored in subdirectories within the main
101 "libraries" directory, or within your personal *application/libraries*
102 directory. To load a file located in a subdirectory, simply include the
103 path, relative to the "libraries" directory. For example, if you have
104 file located at::
105
106 libraries/flavors/Chocolate.php
107
108 You will load it using::
109
110 $this->load->library('flavors/chocolate');
111
112 You may nest the file in as many subdirectories as you want.
113
114 Additionally, multiple libraries can be loaded at the same time by
115 passing an array of libraries to the load method.
116 ::
117
118 $this->load->library(array('email', 'table'));
119
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600120 **Setting options**
Andrey Andreev05563962014-01-06 12:28:59 +0200121
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
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600140 **Assigning a Library to a different object name**
Andrey Andreev05563962014-01-06 12:28:59 +0200141
142 If the third (optional) parameter is blank, the library will usually be
143 assigned to an object with the same name as the library. For example, if
144 the library is named Calendar, it will be assigned to a variable named
145 ``$this->calendar``.
146
147 If you prefer to set your own class names you can pass its value to the
148 third parameter::
149
150 $this->load->library('calendar', NULL, 'my_calendar');
151
152 // Calendar class is now accessed using:
153 $this->my_calendar
154
155 Please take note, when multiple libraries are supplied in an array for
156 the first parameter, this parameter is discarded.
157
158 .. method:: driver($library[, $params = NULL[, $object_name]])
159
Andrey Andreev28c2c972014-02-08 04:27:48 +0200160 :param mixed $library: Library name as a string or an array with multiple libraries
161 :param array $params: Optional array of parameters to pass to the loaded library's constructor
162 :param string $object_name: Optional object name to assign the library to
163 :returns: CI_Loader instance (method chaining)
164 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200165
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
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600187 **Setting options**
Andrey Andreev05563962014-01-06 12:28:59 +0200188
189 The second (optional) parameter allows you to optionally pass
190 configuration settings. You will typically pass these as an array::
191
192 $config = array(
193 'sess_driver' => 'cookie',
194 'sess_encrypt_cookie' => true,
195 'encryption_key' => 'mysecretkey'
196 );
197
198 $this->load->driver('session', $config);
199
200 Config options can usually also be set via a config file. Each library
201 is explained in detail in its own page, so please read the information
202 regarding each one you would like to use.
203
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600204 **Assigning a Driver to a different object name**
Andrey Andreev05563962014-01-06 12:28:59 +0200205
206 If the third (optional) parameter is blank, the library will be assigned
207 to an object with the same name as the parent class. For example, if
208 the library is named Session, it will be assigned to a variable named
209 ``$this->session``.
210
211 If you prefer to set your own class names you can pass its value to the
212 third parameter::
213
214 $this->load->library('session', '', 'my_session');
215
216 // Session class is now accessed using:
217 $this->my_session
218
219 .. method:: view($view[, $vars = array()[, return = FALSE]])
220
Andrey Andreev28c2c972014-02-08 04:27:48 +0200221 :param string $view: View name
222 :param array $vars: An associative array of variables
223 :param bool $return: Whether to return the loaded view
224 :returns: View content string if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
225 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200226
227 This method is used to load your View files. If you haven't read the
228 :doc:`Views <../general/views>` section of the user guide it is
229 recommended that you do since it shows you how this method is
230 typically used.
231
232 The first parameter is required. It is the name of the view file you
233 would like to load.
234
235 .. note:: The .php file extension does not need to be specified unless
236 you use something other than .php.
237
238 The second **optional** parameter can take an associative array or an
239 object as input, which it runs through the PHP
240 `extract() <http://www.php.net/extract>`_ function to convert to variables
241 that can be used in your view files. Again, read the
242 :doc:`Views <../general/views>` page to learn how this might be useful.
243
244 The third **optional** parameter lets you change the behavior of the
245 method so that it returns data as a string rather than sending it to
246 your browser. This can be useful if you want to process the data in some
247 way. If you set the parameter to TRUE (boolean) it will return data. The
248 default behavior is FALSE, which sends it to your browser. Remember to
249 assign it to a variable if you want the data returned::
250
251 $string = $this->load->view('myfile', '', TRUE);
252
253 .. method:: vars($vars[, $val = ''])
254
Andrey Andreev28c2c972014-02-08 04:27:48 +0200255 :param mixed $vars: An array of variables or a single variable name
256 :param mixed $val: Optional variable value
257 :returns: CI_Loader instance (method chaining)
258 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200259
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
Andrey Andreev28c2c972014-02-08 04:27:48 +0200272 :param string $key: Variable name key
273 :returns: Value if key is found, NULL if not
274 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200275
276 This method checks the associative array of variables available to
277 your views. This is useful if for any reason a var is set in a library
278 or another controller method using ``$this->load->vars()``.
279
280 .. method:: get_vars()
281
Andrey Andreev28c2c972014-02-08 04:27:48 +0200282 :returns: An array of all assigned view variables
283 :rtype: array
Andrey Andreev05563962014-01-06 12:28:59 +0200284
285 This method retrieves all variables available to your views.
286
Andrey Andreevea801ab2014-01-20 15:03:43 +0200287 .. method:: clear_vars()
288
Andrey Andreev28c2c972014-02-08 04:27:48 +0200289 :returns: CI_Loader instance (method chaining)
290 :rtype: CI_Loader
Andrey Andreevea801ab2014-01-20 15:03:43 +0200291
292 Clears cached view variables.
293
Andrey Andreev05563962014-01-06 12:28:59 +0200294 .. method:: model($model[, $name = ''[, $db_conn = FALSE]])
295
Andrey Andreev28c2c972014-02-08 04:27:48 +0200296 :param mixed $model: Model name or an array containing multiple models
297 :param string $name: Optional object name to assign the model to
298 :param string $db_conn: Optional database configuration group to load
299 :returns: CI_Loader instance (method chaining)
300 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200301
302 ::
303
304 $this->load->model('model_name');
305
306
307 If your model is located in a subdirectory, include the relative path
308 from your models directory. For example, if you have a model located at
Andrey Andreevea801ab2014-01-20 15:03:43 +0200309 *application/models/blog/Queries.php* you'll load it using::
Andrey Andreev05563962014-01-06 12:28:59 +0200310
311 $this->load->model('blog/queries');
312
313 If you would like your model assigned to a different object name you can
314 specify it via the second parameter of the loading method::
315
316 $this->load->model('model_name', 'fubar');
317 $this->fubar->method();
318
319 .. method:: database([$params = ''[, $return = FALSE[, $query_builder = NULL]]])
320
Andrey Andreev28c2c972014-02-08 04:27:48 +0200321 :param mixed $params: Database group name or configuration options
322 :param bool $return: Whether to return the loaded database object
323 :param bool $query_builder: Whether to load the Query Builder
324 :returns: Loaded CI_DB instance or FALSE on failure if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
325 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200326
327 This method lets you load the database class. The two parameters are
328 **optional**. Please see the :doc:`database <../database/index>`
329 section for more info.
330
331 .. method:: dbforge([$db = NULL[, $return = FALSE]])
332
Andrey Andreev28c2c972014-02-08 04:27:48 +0200333 :param object $db: Database object
334 :param bool $return: Whether to return the Database Forge instance
335 :returns: Loaded CI_DB_forge instance if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
336 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200337
338 Loads the :doc:`Database Forge <../database/forge>` class, please refer
339 to that manual for more info.
340
341 .. method:: dbutil([$db = NULL[, $return = FALSE]])
342
Andrey Andreev28c2c972014-02-08 04:27:48 +0200343 :param object $db: Database object
344 :param bool $return: Whether to return the Database Utilities instance
345 :returns: Loaded CI_DB_utility instance if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
346 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200347
348 Loads the :doc:`Database Utilities <../database/utilities>` class, please
349 refer to that manual for more info.
350
351 .. method:: helper($helpers)
352
Andrey Andreev28c2c972014-02-08 04:27:48 +0200353 :param mixed $helpers: Helper name as a string or an array containing multiple helpers
354 :returns: CI_Loader instance (method chaining)
355 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200356
357 This method loads helper files, where file_name is the name of the
358 file, without the _helper.php extension.
359
360 .. method:: file($path[, $return = FALSE])
361
Andrey Andreev28c2c972014-02-08 04:27:48 +0200362 :param string $path: File path
363 :param bool $return: Whether to return the loaded file
364 :returns: File contents if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
365 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200366
367 This is a generic file loading method. Supply the filepath and name in
368 the first parameter and it will open and read the file. By default the
369 data is sent to your browser, just like a View file, but if you set the
370 second parameter to boolean TRUE it will instead return the data as a
371 string.
372
373 .. method:: language($files[, $lang = ''])
374
Andrey Andreev28c2c972014-02-08 04:27:48 +0200375 :param mixed $files: Language file name or an array of multiple language files
376 :param string $lang: Language name
377 :returns: CI_Loader instance (method chaining)
378 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200379
380 This method is an alias of the :doc:`language loading
381 method <language>`: ``$this->lang->load()``.
382
383 .. method:: config($file[, $use_sections = FALSE[, $fail_gracefully = FALSE]])
384
Andrey Andreev28c2c972014-02-08 04:27:48 +0200385 :param string $file: Configuration file name
386 :param bool $use_sections: Whether configuration values should be loaded into their own section
387 :param bool $fail_gracefully: Whether to just return FALSE in case of failure
388 :returns: TRUE on success, FALSE on failure
389 :rtype: bool
Andrey Andreev05563962014-01-06 12:28:59 +0200390
391 This method is an alias of the :doc:`config file loading
392 method <config>`: ``$this->config->load()``
393
Andrey Andreevea801ab2014-01-20 15:03:43 +0200394 .. method:: is_loaded($class)
395
Andrey Andreev28c2c972014-02-08 04:27:48 +0200396 :param string $class: Class name
397 :returns: Singleton property name if found, FALSE if not
398 :rtype: mixed
Andrey Andreevea801ab2014-01-20 15:03:43 +0200399
400 Allows you to check if a class has already been loaded or not.
401
402 .. note:: The word "class" here refers to libraries and drivers.
403
404 If the requested class has been loaded, the method returns its assigned
405 name in the CI Super-object and FALSE if it's not::
406
407 $this->load->library('form_validation');
408 $this->load->is_loaded('Form_validation'); // returns 'form_validation'
409
410 $this->load->is_loaded('Nonexistent_library'); // returns FALSE
411
412 .. important:: If you have more than one instance of a class (assigned to
413 different properties), then the first one will be returned.
414
415 ::
416
417 $this->load->library('form_validation', $config, 'fv');
418 $this->load->library('form_validation');
419
420 $this->load->is_loaded('Form_validation'); // returns 'fv'
421
Andrey Andreev05563962014-01-06 12:28:59 +0200422 .. method:: add_package_path($path[, $view_cascade = TRUE])
423
Andrey Andreev28c2c972014-02-08 04:27:48 +0200424 :param string $path: Path to add
425 :param bool $view_cascade: Whether to use cascading views
426 :returns: CI_Loader instance (method chaining)
427 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200428
429 Adding a package path instructs the Loader class to prepend a given path
430 for subsequent requests for resources. As an example, the "Foo Bar"
431 application package above has a library named Foo_bar.php. In our
432 controller, we'd do the following::
433
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200434 $this->load->add_package_path(APPPATH.'third_party/foo_bar/')
435 ->library('foo_bar');
Andrey Andreev05563962014-01-06 12:28:59 +0200436
437 .. method:: remove_package_path([$path = ''])
438
Andrey Andreev28c2c972014-02-08 04:27:48 +0200439 :param string $path: Path to remove
440 :returns: CI_Loader instance (method chaining)
441 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200442
443 When your controller is finished using resources from an application
444 package, and particularly if you have other application packages you
445 want to work with, you may wish to remove the package path so the Loader
446 no longer looks in that directory for resources. To remove the last path
447 added, simply call the method with no parameters.
448
449 Or to remove a specific package path, specify the same path previously
450 given to ``add_package_path()`` for a package.::
451
452 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
453
454 .. method:: get_package_paths([$include_base = TRUE])
455
Andrey Andreev28c2c972014-02-08 04:27:48 +0200456 :param bool $include_base: Whether to include BASEPATH
457 :returns: An array of package paths
458 :rtype: array
Andrey Andreev05563962014-01-06 12:28:59 +0200459
460 Returns all currently available package paths.