blob: 19446a9c8d93801b1a8874c2aa407a4c173ad101 [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 Andreevc26d34f2013-01-28 21:46:08 +020014The following methods are available in this class:
Derek Jones8ede1a22011-10-05 13:34:52 -050015
16$this->load->library('class_name', $config, 'object name')
Andrey Andreevc26d34f2013-01-28 21:46:08 +020017==========================================================
Derek Jones8ede1a22011-10-05 13:34:52 -050018
Andrey Andreevc26d34f2013-01-28 21:46:08 +020019This method is used to load core classes. Where class_name is the
20name of the class you want to load.
21
22.. note:: We use the terms "class" and "library" interchangeably.
Derek Jones8ede1a22011-10-05 13:34:52 -050023
24For example, if you would like to send email with CodeIgniter, the first
25step is to load the email class within your controller::
26
27 $this->load->library('email');
28
29Once loaded, the library will be ready for use, using
Andrey Andreevc26d34f2013-01-28 21:46:08 +020030$this->email->*some_method*().
Derek Jones8ede1a22011-10-05 13:34:52 -050031
32Library files can be stored in subdirectories within the main
Andrey Andreevc26d34f2013-01-28 21:46:08 +020033"libraries" directory, or within your personal application/libraries
34directory. To load a file located in a subdirectory, simply include the
35path, relative to the "libraries" directory. For example, if you have
36file located at::
Derek Jones8ede1a22011-10-05 13:34:52 -050037
Andrey Andreevc26d34f2013-01-28 21:46:08 +020038 libraries/flavors/Chocolate.php
Derek Jones8ede1a22011-10-05 13:34:52 -050039
40You will load it using::
41
42 $this->load->library('flavors/chocolate');
43
44You may nest the file in as many subdirectories as you want.
45
46Additionally, multiple libraries can be loaded at the same time by
Andrey Andreevc26d34f2013-01-28 21:46:08 +020047passing an array of libraries to the load method.
Derek Jones8ede1a22011-10-05 13:34:52 -050048
49::
50
51 $this->load->library(array('email', 'table'));
52
53Setting options
54---------------
55
56The second (optional) parameter allows you to optionally pass
57configuration setting. You will typically pass these as an array::
58
Derek Jones36be9692011-10-05 15:52:41 -050059 $config = array (
Andrey Andreevc26d34f2013-01-28 21:46:08 +020060 'mailtype' => 'html',
61 'charset' => 'utf-8,
62 'priority' => '1'
63 );
Derek Jones36be9692011-10-05 15:52:41 -050064
65 $this->load->library('email', $config);
Derek Jones8ede1a22011-10-05 13:34:52 -050066
67Config options can usually also be set via a config file. Each library
68is explained in detail in its own page, so please read the information
69regarding each one you would like to use.
70
71Please take note, when multiple libraries are supplied in an array for
72the first parameter, each will receive the same parameter information.
73
74Assigning a Library to a different object name
75----------------------------------------------
76
77If the third (optional) parameter is blank, the library will usually be
78assigned to an object with the same name as the library. For example, if
dchill423169f262012-08-11 20:12:05 -040079the library is named Calendar, it will be assigned to a variable named
80$this->calendar.
81
82If you prefer to set your own class names you can pass its value to the
83third parameter::
84
85 $this->load->library('calendar', '', 'my_calendar');
86
dchill42b3816b72012-08-13 09:47:58 -040087 // Calendar class is now accessed using:
dchill423169f262012-08-11 20:12:05 -040088 $this->my_calendar
89
90Please take note, when multiple libraries are supplied in an array for
91the first parameter, this parameter is discarded.
92
93$this->load->driver('parent_name', $config, 'object name')
Andrey Andreevc26d34f2013-01-28 21:46:08 +020094==========================================================
dchill423169f262012-08-11 20:12:05 -040095
Andrey Andreevc26d34f2013-01-28 21:46:08 +020096This method is used to load driver libraries. Where parent_name is the
dchill423169f262012-08-11 20:12:05 -040097name of the parent class you want to load.
98
99As an example, if you would like to use sessions with CodeIgniter, the first
100step is to load the session driver within your controller::
101
102 $this->load->driver('session');
103
104Once loaded, the library will be ready for use, using
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200105$this->session->*some_method*().
dchill423169f262012-08-11 20:12:05 -0400106
107Driver files must be stored in a subdirectory within the main
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200108"libraries" directory, or within your personal application/libraries
109directory. The subdirectory must match the parent class name. Read the
dchill423169f262012-08-11 20:12:05 -0400110:doc:`Drivers <../general/drivers>` description for details.
111
112Additionally, multiple driver libraries can be loaded at the same time by
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200113passing an array of drivers to the load method.
dchill423169f262012-08-11 20:12:05 -0400114
115::
116
117 $this->load->driver(array('session', 'cache'));
118
119Setting options
120---------------
121
122The second (optional) parameter allows you to optionally pass
123configuration settings. You will typically pass these as an array::
124
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200125 $config = array(
126 'sess_driver' => 'cookie',
127 'sess_encrypt_cookie' => true,
128 'encryption_key' => 'mysecretkey'
129 );
dchill423169f262012-08-11 20:12:05 -0400130
131 $this->load->driver('session', $config);
132
133Config options can usually also be set via a config file. Each library
134is explained in detail in its own page, so please read the information
135regarding each one you would like to use.
136
137Assigning a Driver to a different object name
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200138---------------------------------------------
dchill423169f262012-08-11 20:12:05 -0400139
140If the third (optional) parameter is blank, the library will be assigned
141to an object with the same name as the parent class. For example, if
Derek Jones8ede1a22011-10-05 13:34:52 -0500142the library is named Session, it will be assigned to a variable named
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200143``$this->session``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
145If you prefer to set your own class names you can pass its value to the
146third parameter::
147
Derek Jones36be9692011-10-05 15:52:41 -0500148 $this->load->library('session', '', 'my_session');
149
150 // Session class is now accessed using:
Derek Jones36be9692011-10-05 15:52:41 -0500151 $this->my_session
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200153.. note:: Driver libraries may also be loaded with the ``library()`` method,
154 but it is faster to use ``driver()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200156$this->load->view('file_name', $data, TRUE/FALSE)
157=================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200159This method is used to load your View files. If you haven't read the
Derek Jones8ede1a22011-10-05 13:34:52 -0500160:doc:`Views <../general/views>` section of the user guide it is
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200161recommended that you do since it shows you how this method is
Derek Jones8ede1a22011-10-05 13:34:52 -0500162typically used.
163
164The first parameter is required. It is the name of the view file you
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200165would like to load.
166
167.. note:: The .php file extension does not need to be specified unless
168 you use something other than .php.
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
170The second **optional** parameter can take an associative array or an
171object as input, which it runs through the PHP
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200172`extract() <http://www.php.net/extract>`_ function to convert to variables
Derek Jones8ede1a22011-10-05 13:34:52 -0500173that can be used in your view files. Again, read the
174:doc:`Views <../general/views>` page to learn how this might be useful.
175
176The third **optional** parameter lets you change the behavior of the
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200177method so that it returns data as a string rather than sending it to
Derek Jones8ede1a22011-10-05 13:34:52 -0500178your browser. This can be useful if you want to process the data in some
179way. If you set the parameter to true (boolean) it will return data. The
180default behavior is false, which sends it to your browser. Remember to
181assign it to a variable if you want the data returned::
182
183 $string = $this->load->view('myfile', '', true);
184
Alex Bilbie149c0772012-06-02 11:23:41 +0100185$this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500186==================================
187
188::
189
Alex Bilbie149c0772012-06-02 11:23:41 +0100190 $this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
192
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200193If your model is located in a subdirectory, include the relative path
194from your models directory. For example, if you have a model located at
Andrey Andreev20292312013-07-22 14:29:10 +0300195application/models/blog/Queries.php you'll load it using::
Derek Jones8ede1a22011-10-05 13:34:52 -0500196
197 $this->load->model('blog/queries');
198
Derek Jones8ede1a22011-10-05 13:34:52 -0500199If you would like your model assigned to a different object name you can
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200200specify it via the second parameter of the loading method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500201
Alex Bilbie149c0772012-06-02 11:23:41 +0100202 $this->load->model('model_name', 'fubar');
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200203 $this->fubar->method();
Derek Jones36be9692011-10-05 15:52:41 -0500204
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200205$this->load->database('options', TRUE/FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500206============================================
207
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200208This method lets you load the database class. The two parameters are
Derek Jones8ede1a22011-10-05 13:34:52 -0500209**optional**. Please see the :doc:`database <../database/index>`
210section for more info.
211
212$this->load->vars($array)
213=========================
214
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200215This method takes an associative array as input and generates
Derek Jones8ede1a22011-10-05 13:34:52 -0500216variables using the PHP `extract <http://www.php.net/extract>`_
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200217method. This method produces the same result as using the second
218parameter of the ``$this->load->view()`` method above. The reason you
219might want to use this method independently is if you would like to
Derek Jones8ede1a22011-10-05 13:34:52 -0500220set some global variables in the constructor of your controller and have
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200221them become available in any view file loaded from any method. You can
222have multiple calls to this method. The data get cached and merged
Derek Jones8ede1a22011-10-05 13:34:52 -0500223into one array for conversion to variables.
224
225$this->load->get_var($key)
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200226==========================
Derek Jones8ede1a22011-10-05 13:34:52 -0500227
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200228This method checks the associative array of variables available to
Derek Jones8ede1a22011-10-05 13:34:52 -0500229your views. This is useful if for any reason a var is set in a library
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200230or another controller method using ``$this->load->vars()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500231
Shane Pearson81dd2232011-11-18 20:49:35 -0600232$this->load->get_vars()
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200233=======================
Shane Pearson81dd2232011-11-18 20:49:35 -0600234
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200235This method retrieves all variables available to your views.
Shane Pearson81dd2232011-11-18 20:49:35 -0600236
Derek Jones8ede1a22011-10-05 13:34:52 -0500237$this->load->helper('file_name')
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200238================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500239
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200240This method loads helper files, where file_name is the name of the
Derek Jones8ede1a22011-10-05 13:34:52 -0500241file, without the _helper.php extension.
242
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200243$this->load->file('filepath/filename', TRUE/FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500244==================================================
245
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200246This is a generic file loading method. Supply the filepath and name in
Derek Jones8ede1a22011-10-05 13:34:52 -0500247the first parameter and it will open and read the file. By default the
248data is sent to your browser, just like a View file, but if you set the
249second parameter to true (boolean) it will instead return the data as a
250string.
251
252$this->load->language('file_name')
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200253==================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200255This method is an alias of the :doc:`language loading
256method <language>`: ``$this->lang->load()``
Derek Jones8ede1a22011-10-05 13:34:52 -0500257
258$this->load->config('file_name')
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200259================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500260
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200261This method is an alias of the :doc:`config file loading
262method <config>`: ``$this->config->load()``
Derek Jones8ede1a22011-10-05 13:34:52 -0500263
Andrey Andreev519f87a2013-07-23 17:16:10 +0300264$this->load->is_loaded('library_name')
265======================================
266
267The ``is_loaded()`` method allows you to check if a class has already
268been loaded or not.
269
270.. note:: The word "class" here refers to libraries and drivers.
271
272If the requested class has been loaded, the method returns its assigned
273name in the CI Super-object and FALSE if it's not::
274
275 $this->load->library('form_validation');
276 $this->load->is_loaded('Form_validation'); // returns 'form_validation'
277
278 $this->load->is_loaded('Nonexistent_library'); // returns FALSE
279
280.. important:: If you have more than one instance of a class (assigned to
281 different properties), then the first one will be returned.
282
283::
284
285 $this->load->library('form_validation', $config, 'fv');
286 $this->load->library('form_validation');
287
288 $this->load->is_loaded('Form_validation'); // returns 'fv'
289
Derek Jones8ede1a22011-10-05 13:34:52 -0500290Application "Packages"
291======================
292
293An application package allows for the easy distribution of complete sets
294of resources in a single directory, complete with its own libraries,
295models, helpers, config, and language files. It is recommended that
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200296these packages be placed in the application/third_party directory. Below
Derek Jones8ede1a22011-10-05 13:34:52 -0500297is a sample map of an package directory
298
299Sample Package "Foo Bar" Directory Map
300======================================
301
302The following is an example of a directory for an application package
303named "Foo Bar".
304
305::
306
Derek Jones36be9692011-10-05 15:52:41 -0500307 /application/third_party/foo_bar
308
309 config/
310 helpers/
311 language/
312 libraries/
313 models/
Derek Jones8ede1a22011-10-05 13:34:52 -0500314
315Whatever the purpose of the "Foo Bar" application package, it has its
316own config files, helpers, language files, libraries, and models. To use
317these resources in your controllers, you first need to tell the Loader
318that you are going to be loading resources from a package, by adding the
319package path.
320
321$this->load->add_package_path()
322---------------------------------
323
324Adding a package path instructs the Loader class to prepend a given path
325for subsequent requests for resources. As an example, the "Foo Bar"
326application package above has a library named Foo_bar.php. In our
327controller, we'd do the following::
328
Derek Jones36be9692011-10-05 15:52:41 -0500329 $this->load->add_package_path(APPPATH.'third_party/foo_bar/');
330 $this->load->library('foo_bar');
Derek Jones8ede1a22011-10-05 13:34:52 -0500331
332$this->load->remove_package_path()
333------------------------------------
334
335When your controller is finished using resources from an application
336package, and particularly if you have other application packages you
337want to work with, you may wish to remove the package path so the Loader
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200338no longer looks in that directory for resources. To remove the last path
Derek Jones8ede1a22011-10-05 13:34:52 -0500339added, simply call the method with no parameters.
340
341$this->load->remove_package_path()
342------------------------------------
343
344Or to remove a specific package path, specify the same path previously
345given to add_package_path() for a package.::
346
347 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
348
349Package view files
350------------------
351
352By Default, package view files paths are set when add_package_path()
353is called. View paths are looped through, and once a match is
354encountered that view is loaded.
355
356In this instance, it is possible for view naming collisions within
357packages to occur, and possibly the incorrect package being loaded. To
358ensure against this, set an optional second parameter of FALSE when
359calling add_package_path().
360
361::
362
363 $this->load->add_package_path(APPPATH.'my_app', FALSE);
364 $this->load->view('my_app_index'); // Loads
365 $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE
366
367 // Reset things
368 $this->load->remove_package_path(APPPATH.'my_app');
369
370 // Again without the second parameter:
W. Kristiantoa5e329f2012-09-23 22:34:18 +0700371 $this->load->add_package_path(APPPATH.'my_app');
Derek Jones8ede1a22011-10-05 13:34:52 -0500372 $this->load->view('my_app_index'); // Loads
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200373 $this->load->view('welcome_message'); // Loads