blob: 91db5afbd86d52111d92fa39002118a9c515e317 [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
Andrey Andreevb69cbcb2013-10-16 13:59:43 +0300237$this->load->clear_vars()
238=========================
239
240Clears cached view variables.
241
Derek Jones8ede1a22011-10-05 13:34:52 -0500242$this->load->helper('file_name')
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200243================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200245This method loads helper files, where file_name is the name of the
Derek Jones8ede1a22011-10-05 13:34:52 -0500246file, without the _helper.php extension.
247
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200248$this->load->file('filepath/filename', TRUE/FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500249==================================================
250
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200251This is a generic file loading method. Supply the filepath and name in
Derek Jones8ede1a22011-10-05 13:34:52 -0500252the first parameter and it will open and read the file. By default the
253data is sent to your browser, just like a View file, but if you set the
254second parameter to true (boolean) it will instead return the data as a
255string.
256
257$this->load->language('file_name')
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200258==================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500259
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200260This method is an alias of the :doc:`language loading
261method <language>`: ``$this->lang->load()``
Derek Jones8ede1a22011-10-05 13:34:52 -0500262
263$this->load->config('file_name')
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200264================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500265
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200266This method is an alias of the :doc:`config file loading
267method <config>`: ``$this->config->load()``
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
Andrey Andreev519f87a2013-07-23 17:16:10 +0300269$this->load->is_loaded('library_name')
270======================================
271
272The ``is_loaded()`` method allows you to check if a class has already
273been loaded or not.
274
275.. note:: The word "class" here refers to libraries and drivers.
276
277If the requested class has been loaded, the method returns its assigned
278name in the CI Super-object and FALSE if it's not::
279
280 $this->load->library('form_validation');
281 $this->load->is_loaded('Form_validation'); // returns 'form_validation'
282
283 $this->load->is_loaded('Nonexistent_library'); // returns FALSE
284
285.. important:: If you have more than one instance of a class (assigned to
286 different properties), then the first one will be returned.
287
288::
289
290 $this->load->library('form_validation', $config, 'fv');
291 $this->load->library('form_validation');
292
293 $this->load->is_loaded('Form_validation'); // returns 'fv'
294
Derek Jones8ede1a22011-10-05 13:34:52 -0500295Application "Packages"
296======================
297
298An application package allows for the easy distribution of complete sets
299of resources in a single directory, complete with its own libraries,
300models, helpers, config, and language files. It is recommended that
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200301these packages be placed in the application/third_party directory. Below
Derek Jones8ede1a22011-10-05 13:34:52 -0500302is a sample map of an package directory
303
304Sample Package "Foo Bar" Directory Map
305======================================
306
307The following is an example of a directory for an application package
308named "Foo Bar".
309
310::
311
Derek Jones36be9692011-10-05 15:52:41 -0500312 /application/third_party/foo_bar
313
314 config/
315 helpers/
316 language/
317 libraries/
318 models/
Derek Jones8ede1a22011-10-05 13:34:52 -0500319
320Whatever the purpose of the "Foo Bar" application package, it has its
321own config files, helpers, language files, libraries, and models. To use
322these resources in your controllers, you first need to tell the Loader
323that you are going to be loading resources from a package, by adding the
324package path.
325
326$this->load->add_package_path()
327---------------------------------
328
329Adding a package path instructs the Loader class to prepend a given path
330for subsequent requests for resources. As an example, the "Foo Bar"
331application package above has a library named Foo_bar.php. In our
332controller, we'd do the following::
333
Derek Jones36be9692011-10-05 15:52:41 -0500334 $this->load->add_package_path(APPPATH.'third_party/foo_bar/');
335 $this->load->library('foo_bar');
Derek Jones8ede1a22011-10-05 13:34:52 -0500336
337$this->load->remove_package_path()
338------------------------------------
339
340When your controller is finished using resources from an application
341package, and particularly if you have other application packages you
342want to work with, you may wish to remove the package path so the Loader
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200343no longer looks in that directory for resources. To remove the last path
Derek Jones8ede1a22011-10-05 13:34:52 -0500344added, simply call the method with no parameters.
345
346$this->load->remove_package_path()
347------------------------------------
348
349Or to remove a specific package path, specify the same path previously
350given to add_package_path() for a package.::
351
352 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
353
354Package view files
355------------------
356
357By Default, package view files paths are set when add_package_path()
358is called. View paths are looped through, and once a match is
359encountered that view is loaded.
360
361In this instance, it is possible for view naming collisions within
362packages to occur, and possibly the incorrect package being loaded. To
363ensure against this, set an optional second parameter of FALSE when
364calling add_package_path().
365
366::
367
368 $this->load->add_package_path(APPPATH.'my_app', FALSE);
369 $this->load->view('my_app_index'); // Loads
370 $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE
371
372 // Reset things
373 $this->load->remove_package_path(APPPATH.'my_app');
374
375 // Again without the second parameter:
W. Kristiantoa5e329f2012-09-23 22:34:18 +0700376 $this->load->add_package_path(APPPATH.'my_app');
Derek Jones8ede1a22011-10-05 13:34:52 -0500377 $this->load->view('my_app_index'); // Loads
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200378 $this->load->view('welcome_message'); // Loads