blob: 615aba1c22c8541cb1f87ef7161fe064a0904d01 [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
14The following functions are available in this class:
15
16$this->load->library('class_name', $config, 'object name')
17===========================================================
18
19This function is used to load core classes. Where class_name is the
20name of the class you want to load. Note: We use the terms "class" and
21"library" interchangeably.
22
23For example, if you would like to send email with CodeIgniter, the first
24step is to load the email class within your controller::
25
26 $this->load->library('email');
27
28Once loaded, the library will be ready for use, using
29$this->email->*some_function*().
30
31Library files can be stored in subdirectories within the main
32"libraries" folder, or within your personal application/libraries
33folder. To load a file located in a subdirectory, simply include the
34path, relative to the "libraries" folder. For example, if you have file
35located at::
36
37 libraries/flavors/chocolate.php
38
39You will load it using::
40
41 $this->load->library('flavors/chocolate');
42
43You may nest the file in as many subdirectories as you want.
44
45Additionally, multiple libraries can be loaded at the same time by
46passing an array of libraries to the load function.
47
48::
49
50 $this->load->library(array('email', 'table'));
51
52Setting options
53---------------
54
55The second (optional) parameter allows you to optionally pass
56configuration setting. You will typically pass these as an array::
57
Derek Jones36be9692011-10-05 15:52:41 -050058 $config = array (
59 'mailtype' => 'html',
60 'charset' => 'utf-8,
61 'priority' => '1'
62 );
63
64 $this->load->library('email', $config);
Derek Jones8ede1a22011-10-05 13:34:52 -050065
66Config options can usually also be set via a config file. Each library
67is explained in detail in its own page, so please read the information
68regarding each one you would like to use.
69
70Please take note, when multiple libraries are supplied in an array for
71the first parameter, each will receive the same parameter information.
72
73Assigning a Library to a different object name
74----------------------------------------------
75
76If the third (optional) parameter is blank, the library will usually be
77assigned to an object with the same name as the library. For example, if
dchill423169f262012-08-11 20:12:05 -040078the library is named Calendar, it will be assigned to a variable named
79$this->calendar.
80
81If you prefer to set your own class names you can pass its value to the
82third parameter::
83
84 $this->load->library('calendar', '', 'my_calendar');
85
dchill42b3816b72012-08-13 09:47:58 -040086 // Calendar class is now accessed using:
dchill423169f262012-08-11 20:12:05 -040087
88 $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')
94===========================================================
95
96This function is used to load driver libraries. Where parent_name is the
97name 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
105$this->session->*some_function*().
106
107Driver files must be stored in a subdirectory within the main
108"libraries" folder, or within your personal application/libraries
109folder. The subdirectory must match the parent class name. Read the
110:doc:`Drivers <../general/drivers>` description for details.
111
112Additionally, multiple driver libraries can be loaded at the same time by
113passing an array of drivers to the load function.
114
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
125 $config = array (
126 'sess_driver' => 'cookie',
127 'sess_encrypt_cookie' => true,
128 'encryption_key' => 'mysecretkey'
129 );
130
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
138----------------------------------------------
139
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
143$this->session.
144
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:
151
152 $this->my_session
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
dchill423169f262012-08-11 20:12:05 -0400154.. note:: Driver libraries may also be loaded with the library() method,
155 but it is faster to use driver()
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
157$this->load->view('file_name', $data, true/false)
158==================================================
159
160This function is used to load your View files. If you haven't read the
161:doc:`Views <../general/views>` section of the user guide it is
162recommended that you do since it shows you how this function is
163typically used.
164
165The first parameter is required. It is the name of the view file you
166would like to load. Note: The .php file extension does not need to be
167specified unless you use something other than .php.
168
169The second **optional** parameter can take an associative array or an
170object as input, which it runs through the PHP
171`extract <http://www.php.net/extract>`_ function to convert to variables
172that can be used in your view files. Again, read the
173:doc:`Views <../general/views>` page to learn how this might be useful.
174
175The third **optional** parameter lets you change the behavior of the
176function so that it returns data as a string rather than sending it to
177your browser. This can be useful if you want to process the data in some
178way. If you set the parameter to true (boolean) it will return data. The
179default behavior is false, which sends it to your browser. Remember to
180assign it to a variable if you want the data returned::
181
182 $string = $this->load->view('myfile', '', true);
183
Alex Bilbie149c0772012-06-02 11:23:41 +0100184$this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500185==================================
186
187::
188
Alex Bilbie149c0772012-06-02 11:23:41 +0100189 $this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
191
192If your model is located in a sub-folder, include the relative path from
193your models folder. For example, if you have a model located at
194application/models/blog/queries.php you'll load it using::
195
196 $this->load->model('blog/queries');
197
198
199If you would like your model assigned to a different object name you can
200specify it via the second parameter of the loading function::
201
Alex Bilbie149c0772012-06-02 11:23:41 +0100202 $this->load->model('model_name', 'fubar');
Derek Jones36be9692011-10-05 15:52:41 -0500203
204 $this->fubar->function();
Derek Jones8ede1a22011-10-05 13:34:52 -0500205
206$this->load->database('options', true/false)
207============================================
208
209This function lets you load the database class. The two parameters are
210**optional**. Please see the :doc:`database <../database/index>`
211section for more info.
212
213$this->load->vars($array)
214=========================
215
216This function takes an associative array as input and generates
217variables using the PHP `extract <http://www.php.net/extract>`_
218function. This function produces the same result as using the second
219parameter of the $this->load->view() function above. The reason you
220might want to use this function independently is if you would like to
221set some global variables in the constructor of your controller and have
222them become available in any view file loaded from any function. You can
223have multiple calls to this function. The data get cached and merged
224into one array for conversion to variables.
225
226$this->load->get_var($key)
227===========================
228
229This function checks the associative array of variables available to
230your views. This is useful if for any reason a var is set in a library
231or another controller method using $this->load->vars().
232
Shane Pearson81dd2232011-11-18 20:49:35 -0600233$this->load->get_vars()
234===========================
235
236This function retrieves all variables available to
237your views.
238
Derek Jones8ede1a22011-10-05 13:34:52 -0500239$this->load->helper('file_name')
240=================================
241
242This function loads helper files, where file_name is the name of the
243file, without the _helper.php extension.
244
245$this->load->file('filepath/filename', true/false)
246==================================================
247
248This is a generic file loading function. Supply the filepath and name in
249the first parameter and it will open and read the file. By default the
250data is sent to your browser, just like a View file, but if you set the
251second parameter to true (boolean) it will instead return the data as a
252string.
253
254$this->load->language('file_name')
255===================================
256
257This function is an alias of the :doc:`language loading
258function <language>`: $this->lang->load()
259
260$this->load->config('file_name')
261=================================
262
263This function is an alias of the :doc:`config file loading
264function <config>`: $this->config->load()
265
266Application "Packages"
267======================
268
269An application package allows for the easy distribution of complete sets
270of resources in a single directory, complete with its own libraries,
271models, helpers, config, and language files. It is recommended that
272these packages be placed in the application/third_party folder. Below
273is a sample map of an package directory
274
275Sample Package "Foo Bar" Directory Map
276======================================
277
278The following is an example of a directory for an application package
279named "Foo Bar".
280
281::
282
Derek Jones36be9692011-10-05 15:52:41 -0500283 /application/third_party/foo_bar
284
285 config/
286 helpers/
287 language/
288 libraries/
289 models/
Derek Jones8ede1a22011-10-05 13:34:52 -0500290
291Whatever the purpose of the "Foo Bar" application package, it has its
292own config files, helpers, language files, libraries, and models. To use
293these resources in your controllers, you first need to tell the Loader
294that you are going to be loading resources from a package, by adding the
295package path.
296
297$this->load->add_package_path()
298---------------------------------
299
300Adding a package path instructs the Loader class to prepend a given path
301for subsequent requests for resources. As an example, the "Foo Bar"
302application package above has a library named Foo_bar.php. In our
303controller, we'd do the following::
304
Derek Jones36be9692011-10-05 15:52:41 -0500305 $this->load->add_package_path(APPPATH.'third_party/foo_bar/');
306 $this->load->library('foo_bar');
Derek Jones8ede1a22011-10-05 13:34:52 -0500307
308$this->load->remove_package_path()
309------------------------------------
310
311When your controller is finished using resources from an application
312package, and particularly if you have other application packages you
313want to work with, you may wish to remove the package path so the Loader
314no longer looks in that folder for resources. To remove the last path
315added, simply call the method with no parameters.
316
317$this->load->remove_package_path()
318------------------------------------
319
320Or to remove a specific package path, specify the same path previously
321given to add_package_path() for a package.::
322
323 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
324
325Package view files
326------------------
327
328By Default, package view files paths are set when add_package_path()
329is called. View paths are looped through, and once a match is
330encountered that view is loaded.
331
332In this instance, it is possible for view naming collisions within
333packages to occur, and possibly the incorrect package being loaded. To
334ensure against this, set an optional second parameter of FALSE when
335calling add_package_path().
336
337::
338
339 $this->load->add_package_path(APPPATH.'my_app', FALSE);
340 $this->load->view('my_app_index'); // Loads
341 $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE
342
343 // Reset things
344 $this->load->remove_package_path(APPPATH.'my_app');
345
346 // Again without the second parameter:
W. Kristiantoa5e329f2012-09-23 22:34:18 +0700347 $this->load->add_package_path(APPPATH.'my_app');
Derek Jones8ede1a22011-10-05 13:34:52 -0500348 $this->load->view('my_app_index'); // Loads
dchill423169f262012-08-11 20:12:05 -0400349 $this->load->view('welcome_message'); // Loads