blob: aadf9740a20729827b58c2ee77cfa1531c51fe8e [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>`,
7:doc:`Helpers <../general/helpers>`,
8:doc:`Models <../general/models>`, or your own files.
9
10.. note:: This class is initialized automatically by the system so there
11 is no need to do it manually.
12
13The following functions are available in this class:
14
15$this->load->library('class_name', $config, 'object name')
16===========================================================
17
18This function is used to load core classes. Where class_name is the
19name of the class you want to load. Note: We use the terms "class" and
20"library" interchangeably.
21
22For example, if you would like to send email with CodeIgniter, the first
23step is to load the email class within your controller::
24
25 $this->load->library('email');
26
27Once loaded, the library will be ready for use, using
28$this->email->*some_function*().
29
30Library files can be stored in subdirectories within the main
31"libraries" folder, or within your personal application/libraries
32folder. To load a file located in a subdirectory, simply include the
33path, relative to the "libraries" folder. For example, if you have file
34located at::
35
36 libraries/flavors/chocolate.php
37
38You will load it using::
39
40 $this->load->library('flavors/chocolate');
41
42You may nest the file in as many subdirectories as you want.
43
44Additionally, multiple libraries can be loaded at the same time by
45passing an array of libraries to the load function.
46
47::
48
49 $this->load->library(array('email', 'table'));
50
51Setting options
52---------------
53
54The second (optional) parameter allows you to optionally pass
55configuration setting. You will typically pass these as an array::
56
Derek Jones36be9692011-10-05 15:52:41 -050057 $config = array (
58 'mailtype' => 'html',
59 'charset' => 'utf-8,
60 'priority' => '1'
61 );
62
63 $this->load->library('email', $config);
Derek Jones8ede1a22011-10-05 13:34:52 -050064
65Config options can usually also be set via a config file. Each library
66is explained in detail in its own page, so please read the information
67regarding each one you would like to use.
68
69Please take note, when multiple libraries are supplied in an array for
70the first parameter, each will receive the same parameter information.
71
72Assigning a Library to a different object name
73----------------------------------------------
74
75If the third (optional) parameter is blank, the library will usually be
76assigned to an object with the same name as the library. For example, if
77the library is named Session, it will be assigned to a variable named
78$this->session.
79
80If you prefer to set your own class names you can pass its value to the
81third parameter::
82
Derek Jones36be9692011-10-05 15:52:41 -050083 $this->load->library('session', '', 'my_session');
84
85 // Session class is now accessed using:
86
87 $this->my_session
Derek Jones8ede1a22011-10-05 13:34:52 -050088
89Please take note, when multiple libraries are supplied in an array for
90the first parameter, this parameter is discarded.
91
92$this->load->view('file_name', $data, true/false)
93==================================================
94
95This function is used to load your View files. If you haven't read the
96:doc:`Views <../general/views>` section of the user guide it is
97recommended that you do since it shows you how this function is
98typically used.
99
100The first parameter is required. It is the name of the view file you
101would like to load. Note: The .php file extension does not need to be
102specified unless you use something other than .php.
103
104The second **optional** parameter can take an associative array or an
105object as input, which it runs through the PHP
106`extract <http://www.php.net/extract>`_ function to convert to variables
107that can be used in your view files. Again, read the
108:doc:`Views <../general/views>` page to learn how this might be useful.
109
110The third **optional** parameter lets you change the behavior of the
111function so that it returns data as a string rather than sending it to
112your browser. This can be useful if you want to process the data in some
113way. If you set the parameter to true (boolean) it will return data. The
114default behavior is false, which sends it to your browser. Remember to
115assign it to a variable if you want the data returned::
116
117 $string = $this->load->view('myfile', '', true);
118
Alex Bilbie149c0772012-06-02 11:23:41 +0100119$this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500120==================================
121
122::
123
Alex Bilbie149c0772012-06-02 11:23:41 +0100124 $this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
126
127If your model is located in a sub-folder, include the relative path from
128your models folder. For example, if you have a model located at
129application/models/blog/queries.php you'll load it using::
130
131 $this->load->model('blog/queries');
132
133
134If you would like your model assigned to a different object name you can
135specify it via the second parameter of the loading function::
136
Alex Bilbie149c0772012-06-02 11:23:41 +0100137 $this->load->model('model_name', 'fubar');
Derek Jones36be9692011-10-05 15:52:41 -0500138
139 $this->fubar->function();
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
141$this->load->database('options', true/false)
142============================================
143
144This function lets you load the database class. The two parameters are
145**optional**. Please see the :doc:`database <../database/index>`
146section for more info.
147
148$this->load->vars($array)
149=========================
150
151This function takes an associative array as input and generates
152variables using the PHP `extract <http://www.php.net/extract>`_
153function. This function produces the same result as using the second
154parameter of the $this->load->view() function above. The reason you
155might want to use this function independently is if you would like to
156set some global variables in the constructor of your controller and have
157them become available in any view file loaded from any function. You can
158have multiple calls to this function. The data get cached and merged
159into one array for conversion to variables.
160
161$this->load->get_var($key)
162===========================
163
164This function checks the associative array of variables available to
165your views. This is useful if for any reason a var is set in a library
166or another controller method using $this->load->vars().
167
Shane Pearson81dd2232011-11-18 20:49:35 -0600168$this->load->get_vars()
169===========================
170
171This function retrieves all variables available to
172your views.
173
Derek Jones8ede1a22011-10-05 13:34:52 -0500174$this->load->helper('file_name')
175=================================
176
177This function loads helper files, where file_name is the name of the
178file, without the _helper.php extension.
179
180$this->load->file('filepath/filename', true/false)
181==================================================
182
183This is a generic file loading function. Supply the filepath and name in
184the first parameter and it will open and read the file. By default the
185data is sent to your browser, just like a View file, but if you set the
186second parameter to true (boolean) it will instead return the data as a
187string.
188
189$this->load->language('file_name')
190===================================
191
192This function is an alias of the :doc:`language loading
193function <language>`: $this->lang->load()
194
195$this->load->config('file_name')
196=================================
197
198This function is an alias of the :doc:`config file loading
199function <config>`: $this->config->load()
200
201Application "Packages"
202======================
203
204An application package allows for the easy distribution of complete sets
205of resources in a single directory, complete with its own libraries,
206models, helpers, config, and language files. It is recommended that
207these packages be placed in the application/third_party folder. Below
208is a sample map of an package directory
209
210Sample Package "Foo Bar" Directory Map
211======================================
212
213The following is an example of a directory for an application package
214named "Foo Bar".
215
216::
217
Derek Jones36be9692011-10-05 15:52:41 -0500218 /application/third_party/foo_bar
219
220 config/
221 helpers/
222 language/
223 libraries/
224 models/
Derek Jones8ede1a22011-10-05 13:34:52 -0500225
226Whatever the purpose of the "Foo Bar" application package, it has its
227own config files, helpers, language files, libraries, and models. To use
228these resources in your controllers, you first need to tell the Loader
229that you are going to be loading resources from a package, by adding the
230package path.
231
232$this->load->add_package_path()
233---------------------------------
234
235Adding a package path instructs the Loader class to prepend a given path
236for subsequent requests for resources. As an example, the "Foo Bar"
237application package above has a library named Foo_bar.php. In our
238controller, we'd do the following::
239
Derek Jones36be9692011-10-05 15:52:41 -0500240 $this->load->add_package_path(APPPATH.'third_party/foo_bar/');
241 $this->load->library('foo_bar');
Derek Jones8ede1a22011-10-05 13:34:52 -0500242
243$this->load->remove_package_path()
244------------------------------------
245
246When your controller is finished using resources from an application
247package, and particularly if you have other application packages you
248want to work with, you may wish to remove the package path so the Loader
249no longer looks in that folder for resources. To remove the last path
250added, simply call the method with no parameters.
251
252$this->load->remove_package_path()
253------------------------------------
254
255Or to remove a specific package path, specify the same path previously
256given to add_package_path() for a package.::
257
258 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
259
260Package view files
261------------------
262
263By Default, package view files paths are set when add_package_path()
264is called. View paths are looped through, and once a match is
265encountered that view is loaded.
266
267In this instance, it is possible for view naming collisions within
268packages to occur, and possibly the incorrect package being loaded. To
269ensure against this, set an optional second parameter of FALSE when
270calling add_package_path().
271
272::
273
274 $this->load->add_package_path(APPPATH.'my_app', FALSE);
275 $this->load->view('my_app_index'); // Loads
276 $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE
277
278 // Reset things
279 $this->load->remove_package_path(APPPATH.'my_app');
280
281 // Again without the second parameter:
282 $this->load->add_package_path(APPPATH.'my_app', TRUE);
283 $this->load->view('my_app_index'); // Loads
284 $this->load->view('welcome_message'); // Loads