blob: 22abb4586db0eda06b3604e70638231b57ef3f3e [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
James L Parry08191be2014-12-20 02:37:13 -080021**********************
Derek Jones8ede1a22011-10-05 13:34:52 -050022Application "Packages"
James L Parry08191be2014-12-20 02:37:13 -080023**********************
Derek Jones8ede1a22011-10-05 13:34:52 -050024
25An application package allows for the easy distribution of complete sets
26of resources in a single directory, complete with its own libraries,
27models, helpers, config, and language files. It is recommended that
Andrey Andreevc26d34f2013-01-28 21:46:08 +020028these packages be placed in the application/third_party directory. Below
Andrey Andreev05563962014-01-06 12:28:59 +020029is a sample map of an package directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050030
31The following is an example of a directory for an application package
32named "Foo Bar".
33
34::
35
Derek Jones36be9692011-10-05 15:52:41 -050036 /application/third_party/foo_bar
37
38 config/
39 helpers/
40 language/
41 libraries/
42 models/
Derek Jones8ede1a22011-10-05 13:34:52 -050043
44Whatever the purpose of the "Foo Bar" application package, it has its
45own config files, helpers, language files, libraries, and models. To use
46these resources in your controllers, you first need to tell the Loader
47that you are going to be loading resources from a package, by adding the
Andrey Andreev05563962014-01-06 12:28:59 +020048package path via the ``add_package_path()`` method.
Derek Jones8ede1a22011-10-05 13:34:52 -050049
50Package view files
51------------------
52
Andrey Andreev05563962014-01-06 12:28:59 +020053By Default, package view files paths are set when ``add_package_path()``
Derek Jones8ede1a22011-10-05 13:34:52 -050054is called. View paths are looped through, and once a match is
55encountered that view is loaded.
56
57In this instance, it is possible for view naming collisions within
58packages to occur, and possibly the incorrect package being loaded. To
59ensure against this, set an optional second parameter of FALSE when
Andrey Andreev05563962014-01-06 12:28:59 +020060calling ``add_package_path()``.
Derek Jones8ede1a22011-10-05 13:34:52 -050061
62::
63
64 $this->load->add_package_path(APPPATH.'my_app', FALSE);
65 $this->load->view('my_app_index'); // Loads
66 $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE
67
68 // Reset things
69 $this->load->remove_package_path(APPPATH.'my_app');
70
71 // Again without the second parameter:
W. Kristiantoa5e329f2012-09-23 22:34:18 +070072 $this->load->add_package_path(APPPATH.'my_app');
Derek Jones8ede1a22011-10-05 13:34:52 -050073 $this->load->view('my_app_index'); // Loads
Andrey Andreev05563962014-01-06 12:28:59 +020074 $this->load->view('welcome_message'); // Loads
75
76***************
77Class Reference
78***************
79
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020080.. php:class:: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +020081
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020082 .. php:method:: library($library[, $params = NULL[, $object_name = NULL]])
Andrey Andreev05563962014-01-06 12:28:59 +020083
Andrey Andreev28c2c972014-02-08 04:27:48 +020084 :param mixed $library: Library name as a string or an array with multiple libraries
85 :param array $params: Optional array of parameters to pass to the loaded library's constructor
86 :param string $object_name: Optional object name to assign the library to
87 :returns: CI_Loader instance (method chaining)
88 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +020089
90 This method is used to load core classes.
91
92 .. note:: We use the terms "class" and "library" interchangeably.
93
94 For example, if you would like to send email with CodeIgniter, the first
95 step is to load the email class within your controller::
96
97 $this->load->library('email');
98
99 Once loaded, the library will be ready for use, using ``$this->email``.
100
101 Library files can be stored in subdirectories within the main
102 "libraries" directory, or within your personal *application/libraries*
103 directory. To load a file located in a subdirectory, simply include the
104 path, relative to the "libraries" directory. For example, if you have
105 file located at::
106
107 libraries/flavors/Chocolate.php
108
109 You will load it using::
110
111 $this->load->library('flavors/chocolate');
112
113 You may nest the file in as many subdirectories as you want.
114
115 Additionally, multiple libraries can be loaded at the same time by
116 passing an array of libraries to the load method.
117 ::
118
119 $this->load->library(array('email', 'table'));
120
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600121 **Setting options**
Andrey Andreev05563962014-01-06 12:28:59 +0200122
123 The second (optional) parameter allows you to optionally pass
124 configuration setting. You will typically pass these as an array::
125
126 $config = array (
127 'mailtype' => 'html',
Andrey Andreev954c4aa2017-07-21 11:51:37 +0300128 'charset' => 'utf-8',
Andrey Andreev05563962014-01-06 12:28:59 +0200129 'priority' => '1'
130 );
131
132 $this->load->library('email', $config);
133
134 Config options can usually also be set via a config file. Each library
135 is explained in detail in its own page, so please read the information
136 regarding each one you would like to use.
137
138 Please take note, when multiple libraries are supplied in an array for
139 the first parameter, each will receive the same parameter information.
140
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600141 **Assigning a Library to a different object name**
Andrey Andreev05563962014-01-06 12:28:59 +0200142
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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200159 .. php:method:: driver($library[, $params = NULL[, $object_name]])
Andrey Andreev05563962014-01-06 12:28:59 +0200160
Andrey Andreev28c2c972014-02-08 04:27:48 +0200161 :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: CI_Loader instance (method chaining)
165 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200166
167 This method is used to load driver libraries, acts very much like the
168 ``library()`` method.
169
170 As an example, if you would like to use sessions with CodeIgniter, the first
171 step is to load the session driver within your controller::
172
173 $this->load->driver('session');
174
175 Once loaded, the library will be ready for use, using ``$this->session``.
176
177 Driver files must be stored in a subdirectory within the main
178 "libraries" directory, or within your personal *application/libraries*
179 directory. The subdirectory must match the parent class name. Read the
180 :doc:`Drivers <../general/drivers>` description for details.
181
182 Additionally, multiple driver libraries can be loaded at the same time by
183 passing an array of drivers to the load method.
184 ::
185
186 $this->load->driver(array('session', 'cache'));
187
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600188 **Setting options**
Andrey Andreev05563962014-01-06 12:28:59 +0200189
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
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600205 **Assigning a Driver to a different object name**
Andrey Andreev05563962014-01-06 12:28:59 +0200206
207 If the third (optional) parameter is blank, the library will be assigned
208 to an object with the same name as the parent class. For example, if
209 the library is named Session, it will be assigned to a variable named
210 ``$this->session``.
211
212 If you prefer to set your own class names you can pass its value to the
213 third parameter::
214
215 $this->load->library('session', '', 'my_session');
216
217 // Session class is now accessed using:
218 $this->my_session
219
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200220 .. php:method:: view($view[, $vars = array()[, return = FALSE]])
Andrey Andreev05563962014-01-06 12:28:59 +0200221
Andrey Andreev28c2c972014-02-08 04:27:48 +0200222 :param string $view: View name
223 :param array $vars: An associative array of variables
224 :param bool $return: Whether to return the loaded view
225 :returns: View content string if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
226 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200227
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
Master Yodabd2a7e42015-03-25 02:36:31 -0700241 `extract() <http://php.net/extract>`_ function to convert to variables
Andrey Andreev05563962014-01-06 12:28:59 +0200242 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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200254 .. php:method:: vars($vars[, $val = ''])
Andrey Andreev05563962014-01-06 12:28:59 +0200255
Andrey Andreev28c2c972014-02-08 04:27:48 +0200256 :param mixed $vars: An array of variables or a single variable name
257 :param mixed $val: Optional variable value
258 :returns: CI_Loader instance (method chaining)
259 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200260
261 This method takes an associative array as input and generates
Master Yodabd2a7e42015-03-25 02:36:31 -0700262 variables using the PHP `extract() <http://php.net/extract>`_
Andrey Andreev05563962014-01-06 12:28:59 +0200263 function. This method produces the same result as using the second
264 parameter of the ``$this->load->view()`` method above. The reason you
265 might want to use this method independently is if you would like to
266 set some global variables in the constructor of your controller and have
267 them become available in any view file loaded from any method. You can
268 have multiple calls to this method. The data get cached and merged
269 into one array for conversion to variables.
270
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200271 .. php:method:: get_var($key)
Andrey Andreev05563962014-01-06 12:28:59 +0200272
Andrey Andreev28c2c972014-02-08 04:27:48 +0200273 :param string $key: Variable name key
274 :returns: Value if key is found, NULL if not
275 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200276
277 This method checks the associative array of variables available to
278 your views. This is useful if for any reason a var is set in a library
279 or another controller method using ``$this->load->vars()``.
280
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200281 .. php:method:: get_vars()
Andrey Andreev05563962014-01-06 12:28:59 +0200282
Andrey Andreev28c2c972014-02-08 04:27:48 +0200283 :returns: An array of all assigned view variables
284 :rtype: array
Andrey Andreev05563962014-01-06 12:28:59 +0200285
286 This method retrieves all variables available to your views.
287
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200288 .. php:method:: clear_vars()
Andrey Andreevea801ab2014-01-20 15:03:43 +0200289
Andrey Andreev28c2c972014-02-08 04:27:48 +0200290 :returns: CI_Loader instance (method chaining)
291 :rtype: CI_Loader
Andrey Andreevea801ab2014-01-20 15:03:43 +0200292
293 Clears cached view variables.
294
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200295 .. php:method:: model($model[, $name = ''[, $db_conn = FALSE]])
Andrey Andreev05563962014-01-06 12:28:59 +0200296
Andrey Andreev28c2c972014-02-08 04:27:48 +0200297 :param mixed $model: Model name or an array containing multiple models
298 :param string $name: Optional object name to assign the model to
299 :param string $db_conn: Optional database configuration group to load
300 :returns: CI_Loader instance (method chaining)
301 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200302
303 ::
304
305 $this->load->model('model_name');
306
307
308 If your model is located in a subdirectory, include the relative path
309 from your models directory. For example, if you have a model located at
Andrey Andreevea801ab2014-01-20 15:03:43 +0200310 *application/models/blog/Queries.php* you'll load it using::
Andrey Andreev05563962014-01-06 12:28:59 +0200311
312 $this->load->model('blog/queries');
313
314 If you would like your model assigned to a different object name you can
315 specify it via the second parameter of the loading method::
316
317 $this->load->model('model_name', 'fubar');
318 $this->fubar->method();
319
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200320 .. php:method:: database([$params = ''[, $return = FALSE[, $query_builder = NULL]]])
Andrey Andreev05563962014-01-06 12:28:59 +0200321
Andrey Andreev28c2c972014-02-08 04:27:48 +0200322 :param mixed $params: Database group name or configuration options
323 :param bool $return: Whether to return the loaded database object
324 :param bool $query_builder: Whether to load the Query Builder
325 :returns: Loaded CI_DB instance or FALSE on failure if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
326 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200327
328 This method lets you load the database class. The two parameters are
329 **optional**. Please see the :doc:`database <../database/index>`
330 section for more info.
331
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200332 .. php:method:: dbforge([$db = NULL[, $return = FALSE]])
Andrey Andreev05563962014-01-06 12:28:59 +0200333
Andrey Andreev28c2c972014-02-08 04:27:48 +0200334 :param object $db: Database object
335 :param bool $return: Whether to return the Database Forge instance
336 :returns: Loaded CI_DB_forge instance if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
337 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200338
339 Loads the :doc:`Database Forge <../database/forge>` class, please refer
340 to that manual for more info.
341
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200342 .. php:method:: dbutil([$db = NULL[, $return = FALSE]])
Andrey Andreev05563962014-01-06 12:28:59 +0200343
Andrey Andreev28c2c972014-02-08 04:27:48 +0200344 :param object $db: Database object
345 :param bool $return: Whether to return the Database Utilities instance
346 :returns: Loaded CI_DB_utility instance if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
347 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200348
349 Loads the :doc:`Database Utilities <../database/utilities>` class, please
350 refer to that manual for more info.
351
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200352 .. php:method:: helper($helpers)
Andrey Andreev05563962014-01-06 12:28:59 +0200353
Andrey Andreev28c2c972014-02-08 04:27:48 +0200354 :param mixed $helpers: Helper name as a string or an array containing multiple helpers
355 :returns: CI_Loader instance (method chaining)
356 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200357
358 This method loads helper files, where file_name is the name of the
359 file, without the _helper.php extension.
360
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200361 .. php:method:: file($path[, $return = FALSE])
Andrey Andreev05563962014-01-06 12:28:59 +0200362
Andrey Andreev28c2c972014-02-08 04:27:48 +0200363 :param string $path: File path
364 :param bool $return: Whether to return the loaded file
365 :returns: File contents if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
366 :rtype: mixed
Andrey Andreev05563962014-01-06 12:28:59 +0200367
368 This is a generic file loading method. Supply the filepath and name in
369 the first parameter and it will open and read the file. By default the
370 data is sent to your browser, just like a View file, but if you set the
371 second parameter to boolean TRUE it will instead return the data as a
372 string.
373
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200374 .. php:method:: language($files[, $lang = ''])
Andrey Andreev05563962014-01-06 12:28:59 +0200375
Andrey Andreev28c2c972014-02-08 04:27:48 +0200376 :param mixed $files: Language file name or an array of multiple language files
377 :param string $lang: Language name
378 :returns: CI_Loader instance (method chaining)
379 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200380
381 This method is an alias of the :doc:`language loading
382 method <language>`: ``$this->lang->load()``.
383
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200384 .. php:method:: config($file[, $use_sections = FALSE[, $fail_gracefully = FALSE]])
Andrey Andreev05563962014-01-06 12:28:59 +0200385
Andrey Andreev28c2c972014-02-08 04:27:48 +0200386 :param string $file: Configuration file name
387 :param bool $use_sections: Whether configuration values should be loaded into their own section
388 :param bool $fail_gracefully: Whether to just return FALSE in case of failure
389 :returns: TRUE on success, FALSE on failure
390 :rtype: bool
Andrey Andreev05563962014-01-06 12:28:59 +0200391
392 This method is an alias of the :doc:`config file loading
393 method <config>`: ``$this->config->load()``
394
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200395 .. php:method:: is_loaded($class)
Andrey Andreevea801ab2014-01-20 15:03:43 +0200396
Andrey Andreev28c2c972014-02-08 04:27:48 +0200397 :param string $class: Class name
398 :returns: Singleton property name if found, FALSE if not
399 :rtype: mixed
Andrey Andreevea801ab2014-01-20 15:03:43 +0200400
401 Allows you to check if a class has already been loaded or not.
402
403 .. note:: The word "class" here refers to libraries and drivers.
404
405 If the requested class has been loaded, the method returns its assigned
406 name in the CI Super-object and FALSE if it's not::
407
408 $this->load->library('form_validation');
409 $this->load->is_loaded('Form_validation'); // returns 'form_validation'
410
411 $this->load->is_loaded('Nonexistent_library'); // returns FALSE
412
413 .. important:: If you have more than one instance of a class (assigned to
414 different properties), then the first one will be returned.
415
416 ::
417
418 $this->load->library('form_validation', $config, 'fv');
419 $this->load->library('form_validation');
420
421 $this->load->is_loaded('Form_validation'); // returns 'fv'
422
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200423 .. php:method:: add_package_path($path[, $view_cascade = TRUE])
Andrey Andreev05563962014-01-06 12:28:59 +0200424
Andrey Andreev28c2c972014-02-08 04:27:48 +0200425 :param string $path: Path to add
426 :param bool $view_cascade: Whether to use cascading views
427 :returns: CI_Loader instance (method chaining)
428 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200429
430 Adding a package path instructs the Loader class to prepend a given path
431 for subsequent requests for resources. As an example, the "Foo Bar"
432 application package above has a library named Foo_bar.php. In our
433 controller, we'd do the following::
434
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200435 $this->load->add_package_path(APPPATH.'third_party/foo_bar/')
436 ->library('foo_bar');
Andrey Andreev05563962014-01-06 12:28:59 +0200437
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200438 .. php:method:: remove_package_path([$path = ''])
Andrey Andreev05563962014-01-06 12:28:59 +0200439
Andrey Andreev28c2c972014-02-08 04:27:48 +0200440 :param string $path: Path to remove
441 :returns: CI_Loader instance (method chaining)
442 :rtype: CI_Loader
Andrey Andreev05563962014-01-06 12:28:59 +0200443
444 When your controller is finished using resources from an application
445 package, and particularly if you have other application packages you
446 want to work with, you may wish to remove the package path so the Loader
447 no longer looks in that directory for resources. To remove the last path
448 added, simply call the method with no parameters.
449
450 Or to remove a specific package path, specify the same path previously
451 given to ``add_package_path()`` for a package.::
452
453 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
454
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200455 .. php:method:: get_package_paths([$include_base = TRUE])
Andrey Andreev05563962014-01-06 12:28:59 +0200456
Andrey Andreev28c2c972014-02-08 04:27:48 +0200457 :param bool $include_base: Whether to include BASEPATH
458 :returns: An array of package paths
459 :rtype: array
Andrey Andreev05563962014-01-06 12:28:59 +0200460
461 Returns all currently available package paths.