blob: 15d9d80fcdefab93ff4f1f1204dc6a01cdf0d5b1 [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
Andrey Andreev0c9d0802014-01-07 13:39:23 +020086 :returns: object
Andrey Andreev05563962014-01-06 12:28:59 +020087
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
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600119 **Setting options**
Andrey Andreev05563962014-01-06 12:28:59 +0200120
121 The second (optional) parameter allows you to optionally pass
122 configuration setting. You will typically pass these as an array::
123
124 $config = array (
125 'mailtype' => 'html',
126 'charset' => 'utf-8,
127 'priority' => '1'
128 );
129
130 $this->load->library('email', $config);
131
132 Config options can usually also be set via a config file. Each library
133 is explained in detail in its own page, so please read the information
134 regarding each one you would like to use.
135
136 Please take note, when multiple libraries are supplied in an array for
137 the first parameter, each will receive the same parameter information.
138
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600139 **Assigning a Library to a different object name**
Andrey Andreev05563962014-01-06 12:28:59 +0200140
141 If the third (optional) parameter is blank, the library will usually be
142 assigned to an object with the same name as the library. For example, if
143 the library is named Calendar, it will be assigned to a variable named
144 ``$this->calendar``.
145
146 If you prefer to set your own class names you can pass its value to the
147 third parameter::
148
149 $this->load->library('calendar', NULL, 'my_calendar');
150
151 // Calendar class is now accessed using:
152 $this->my_calendar
153
154 Please take note, when multiple libraries are supplied in an array for
155 the first parameter, this parameter is discarded.
156
157 .. method:: driver($library[, $params = NULL[, $object_name]])
158
159 :param mixed $library: Library name as a string or an array with multiple libraries
160 :param array $params: Optional array of parameters to pass to the loaded library's constructor
161 :param string $object_name: Optional object name to assign the library to
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200162 :returns: object
Andrey Andreev05563962014-01-06 12:28:59 +0200163
164 This method is used to load driver libraries, acts very much like the
165 ``library()`` method.
166
167 As an example, if you would like to use sessions with CodeIgniter, the first
168 step is to load the session driver within your controller::
169
170 $this->load->driver('session');
171
172 Once loaded, the library will be ready for use, using ``$this->session``.
173
174 Driver files must be stored in a subdirectory within the main
175 "libraries" directory, or within your personal *application/libraries*
176 directory. The subdirectory must match the parent class name. Read the
177 :doc:`Drivers <../general/drivers>` description for details.
178
179 Additionally, multiple driver libraries can be loaded at the same time by
180 passing an array of drivers to the load method.
181 ::
182
183 $this->load->driver(array('session', 'cache'));
184
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600185 **Setting options**
Andrey Andreev05563962014-01-06 12:28:59 +0200186
187 The second (optional) parameter allows you to optionally pass
188 configuration settings. You will typically pass these as an array::
189
190 $config = array(
191 'sess_driver' => 'cookie',
192 'sess_encrypt_cookie' => true,
193 'encryption_key' => 'mysecretkey'
194 );
195
196 $this->load->driver('session', $config);
197
198 Config options can usually also be set via a config file. Each library
199 is explained in detail in its own page, so please read the information
200 regarding each one you would like to use.
201
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600202 **Assigning a Driver to a different object name**
Andrey Andreev05563962014-01-06 12:28:59 +0200203
204 If the third (optional) parameter is blank, the library will be assigned
205 to an object with the same name as the parent class. For example, if
206 the library is named Session, it will be assigned to a variable named
207 ``$this->session``.
208
209 If you prefer to set your own class names you can pass its value to the
210 third parameter::
211
212 $this->load->library('session', '', 'my_session');
213
214 // Session class is now accessed using:
215 $this->my_session
216
217 .. method:: view($view[, $vars = array()[, return = FALSE]])
218
219 :param string $view: View name
220 :param array $vars: An associative array of variables
221 :param bool $return: Whether to return the loaded view
222 :returns: mixed
223
224 This method is used to load your View files. If you haven't read the
225 :doc:`Views <../general/views>` section of the user guide it is
226 recommended that you do since it shows you how this method is
227 typically used.
228
229 The first parameter is required. It is the name of the view file you
230 would like to load.
231
232 .. note:: The .php file extension does not need to be specified unless
233 you use something other than .php.
234
235 The second **optional** parameter can take an associative array or an
236 object as input, which it runs through the PHP
237 `extract() <http://www.php.net/extract>`_ function to convert to variables
238 that can be used in your view files. Again, read the
239 :doc:`Views <../general/views>` page to learn how this might be useful.
240
241 The third **optional** parameter lets you change the behavior of the
242 method so that it returns data as a string rather than sending it to
243 your browser. This can be useful if you want to process the data in some
244 way. If you set the parameter to TRUE (boolean) it will return data. The
245 default behavior is FALSE, which sends it to your browser. Remember to
246 assign it to a variable if you want the data returned::
247
248 $string = $this->load->view('myfile', '', TRUE);
249
250 .. method:: vars($vars[, $val = ''])
251
252 :param mixed $vars: An array of variables or a single variable name
253 :param mixed $val: Optional variable value
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200254 :returns: object
Andrey Andreev05563962014-01-06 12:28:59 +0200255
256 This method takes an associative array as input and generates
257 variables using the PHP `extract() <http://www.php.net/extract>`_
258 function. This method produces the same result as using the second
259 parameter of the ``$this->load->view()`` method above. The reason you
260 might want to use this method independently is if you would like to
261 set some global variables in the constructor of your controller and have
262 them become available in any view file loaded from any method. You can
263 have multiple calls to this method. The data get cached and merged
264 into one array for conversion to variables.
265
266 .. method:: get_var($key)
267
268 :param string $key: Variable name key
269 :returns: mixed
270
271 This method checks the associative array of variables available to
272 your views. This is useful if for any reason a var is set in a library
273 or another controller method using ``$this->load->vars()``.
274
275 .. method:: get_vars()
276
277 :returns: array
278
279 This method retrieves all variables available to your views.
280
Andrey Andreevea801ab2014-01-20 15:03:43 +0200281 .. method:: clear_vars()
282
283 :returns: object
284
285 Clears cached view variables.
286
Andrey Andreev05563962014-01-06 12:28:59 +0200287 .. method:: model($model[, $name = ''[, $db_conn = FALSE]])
288
289 :param mixed $model: Model name or an array containing multiple models
290 :param string $name: Optional object name to assign the model to
291 :param string $db_conn: Optional database configuration group to load
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200292 :returns: object
Andrey Andreev05563962014-01-06 12:28:59 +0200293
294 ::
295
296 $this->load->model('model_name');
297
298
299 If your model is located in a subdirectory, include the relative path
300 from your models directory. For example, if you have a model located at
Andrey Andreevea801ab2014-01-20 15:03:43 +0200301 *application/models/blog/Queries.php* you'll load it using::
Andrey Andreev05563962014-01-06 12:28:59 +0200302
303 $this->load->model('blog/queries');
304
305 If you would like your model assigned to a different object name you can
306 specify it via the second parameter of the loading method::
307
308 $this->load->model('model_name', 'fubar');
309 $this->fubar->method();
310
311 .. method:: database([$params = ''[, $return = FALSE[, $query_builder = NULL]]])
312
313 :param mixed $params: Database group name or configuration options
314 :param bool $return: Whether to return the loaded database object
315 :param bool $query_builder: Whether to load the Query Builder
316 :returns: mixed
317
318 This method lets you load the database class. The two parameters are
319 **optional**. Please see the :doc:`database <../database/index>`
320 section for more info.
321
322 .. method:: dbforge([$db = NULL[, $return = FALSE]])
323
324 :param object $db: Database object
325 :param bool $return: Whether to return the Database Forge instance
326 :returns: mixed
327
328 Loads the :doc:`Database Forge <../database/forge>` class, please refer
329 to that manual for more info.
330
331 .. method:: dbutil([$db = NULL[, $return = FALSE]])
332
333 :param object $db: Database object
334 :param bool $return: Whether to return the Database Utilities instance
335 :returns: mixed
336
337 Loads the :doc:`Database Utilities <../database/utilities>` class, please
338 refer to that manual for more info.
339
340 .. method:: helper($helpers)
341
342 :param mixed $helpers: Helper name as a string or an array containing multiple helpers
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200343 :returns: object
Andrey Andreev05563962014-01-06 12:28:59 +0200344
345 This method loads helper files, where file_name is the name of the
346 file, without the _helper.php extension.
347
348 .. method:: file($path[, $return = FALSE])
349
350 :param string $path: File path
351 :param bool $return: Whether to return the loaded file
352 :returns: mixed
353
354 This is a generic file loading method. Supply the filepath and name in
355 the first parameter and it will open and read the file. By default the
356 data is sent to your browser, just like a View file, but if you set the
357 second parameter to boolean TRUE it will instead return the data as a
358 string.
359
360 .. method:: language($files[, $lang = ''])
361
362 :param mixed $files: Language file name or an array of multiple language files
363 :param string $lang: Language name
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200364 :returns: object
Andrey Andreev05563962014-01-06 12:28:59 +0200365
366 This method is an alias of the :doc:`language loading
367 method <language>`: ``$this->lang->load()``.
368
369 .. method:: config($file[, $use_sections = FALSE[, $fail_gracefully = FALSE]])
370
371 :param string $file: Configuration file name
372 :param bool $use_sections: Whether configuration values should be loaded into their own section
373 :param bool $fail_gracefully: Whether to just return FALSE in case of failure
374 :returns: bool
375
376 This method is an alias of the :doc:`config file loading
377 method <config>`: ``$this->config->load()``
378
Andrey Andreevea801ab2014-01-20 15:03:43 +0200379 .. method:: is_loaded($class)
380
381 :param string $class: Class name
382 :returns: mixed
383
384 Allows you to check if a class has already been loaded or not.
385
386 .. note:: The word "class" here refers to libraries and drivers.
387
388 If the requested class has been loaded, the method returns its assigned
389 name in the CI Super-object and FALSE if it's not::
390
391 $this->load->library('form_validation');
392 $this->load->is_loaded('Form_validation'); // returns 'form_validation'
393
394 $this->load->is_loaded('Nonexistent_library'); // returns FALSE
395
396 .. important:: If you have more than one instance of a class (assigned to
397 different properties), then the first one will be returned.
398
399 ::
400
401 $this->load->library('form_validation', $config, 'fv');
402 $this->load->library('form_validation');
403
404 $this->load->is_loaded('Form_validation'); // returns 'fv'
405
Andrey Andreev05563962014-01-06 12:28:59 +0200406 .. method:: add_package_path($path[, $view_cascade = TRUE])
407
408 :param string $path: Path to add
409 :param bool $view_cascade: Whether to use cascading views
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200410 :returns: object
Andrey Andreev05563962014-01-06 12:28:59 +0200411
412 Adding a package path instructs the Loader class to prepend a given path
413 for subsequent requests for resources. As an example, the "Foo Bar"
414 application package above has a library named Foo_bar.php. In our
415 controller, we'd do the following::
416
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200417 $this->load->add_package_path(APPPATH.'third_party/foo_bar/')
418 ->library('foo_bar');
Andrey Andreev05563962014-01-06 12:28:59 +0200419
420 .. method:: remove_package_path([$path = ''])
421
422 :param string $path: Path to remove
Andrey Andreev0c9d0802014-01-07 13:39:23 +0200423 :returns: object
Andrey Andreev05563962014-01-06 12:28:59 +0200424
425 When your controller is finished using resources from an application
426 package, and particularly if you have other application packages you
427 want to work with, you may wish to remove the package path so the Loader
428 no longer looks in that directory for resources. To remove the last path
429 added, simply call the method with no parameters.
430
431 Or to remove a specific package path, specify the same path previously
432 given to ``add_package_path()`` for a package.::
433
434 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
435
436 .. method:: get_package_paths([$include_base = TRUE])
437
438 :param bool $include_base: Whether to include BASEPATH
439 :returns: array
440
441 Returns all currently available package paths.