blob: 59420e1023b734139c1fb778b4790bc1b6429ffc [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
57 $config = array (                   'mailtype' => 'html',                   'charset'  => 'utf-8,                   'priority' => '1'                ); $this->load->library('email', $config);
58
59Config options can usually also be set via a config file. Each library
60is explained in detail in its own page, so please read the information
61regarding each one you would like to use.
62
63Please take note, when multiple libraries are supplied in an array for
64the first parameter, each will receive the same parameter information.
65
66Assigning a Library to a different object name
67----------------------------------------------
68
69If the third (optional) parameter is blank, the library will usually be
70assigned to an object with the same name as the library. For example, if
71the library is named Session, it will be assigned to a variable named
72$this->session.
73
74If you prefer to set your own class names you can pass its value to the
75third parameter::
76
77 $this->load->library('session', '', 'my_session'); // Session class is now accessed using: $this->my_session
78
79Please take note, when multiple libraries are supplied in an array for
80the first parameter, this parameter is discarded.
81
82$this->load->view('file_name', $data, true/false)
83==================================================
84
85This function is used to load your View files. If you haven't read the
86:doc:`Views <../general/views>` section of the user guide it is
87recommended that you do since it shows you how this function is
88typically used.
89
90The first parameter is required. It is the name of the view file you
91would like to load. Note: The .php file extension does not need to be
92specified unless you use something other than .php.
93
94The second **optional** parameter can take an associative array or an
95object as input, which it runs through the PHP
96`extract <http://www.php.net/extract>`_ function to convert to variables
97that can be used in your view files. Again, read the
98:doc:`Views <../general/views>` page to learn how this might be useful.
99
100The third **optional** parameter lets you change the behavior of the
101function so that it returns data as a string rather than sending it to
102your browser. This can be useful if you want to process the data in some
103way. If you set the parameter to true (boolean) it will return data. The
104default behavior is false, which sends it to your browser. Remember to
105assign it to a variable if you want the data returned::
106
107 $string = $this->load->view('myfile', '', true);
108
109$this->load->model('Model_name');
110==================================
111
112::
113
114 $this->load->model('Model_name');
115
116
117If your model is located in a sub-folder, include the relative path from
118your models folder. For example, if you have a model located at
119application/models/blog/queries.php you'll load it using::
120
121 $this->load->model('blog/queries');
122
123
124If you would like your model assigned to a different object name you can
125specify it via the second parameter of the loading function::
126
127 $this->load->model('Model_name', 'fubar'); $this->fubar->function();
128
129$this->load->database('options', true/false)
130============================================
131
132This function lets you load the database class. The two parameters are
133**optional**. Please see the :doc:`database <../database/index>`
134section for more info.
135
136$this->load->vars($array)
137=========================
138
139This function takes an associative array as input and generates
140variables using the PHP `extract <http://www.php.net/extract>`_
141function. This function produces the same result as using the second
142parameter of the $this->load->view() function above. The reason you
143might want to use this function independently is if you would like to
144set some global variables in the constructor of your controller and have
145them become available in any view file loaded from any function. You can
146have multiple calls to this function. The data get cached and merged
147into one array for conversion to variables.
148
149$this->load->get_var($key)
150===========================
151
152This function checks the associative array of variables available to
153your views. This is useful if for any reason a var is set in a library
154or another controller method using $this->load->vars().
155
156$this->load->helper('file_name')
157=================================
158
159This function loads helper files, where file_name is the name of the
160file, without the _helper.php extension.
161
162$this->load->file('filepath/filename', true/false)
163==================================================
164
165This is a generic file loading function. Supply the filepath and name in
166the first parameter and it will open and read the file. By default the
167data is sent to your browser, just like a View file, but if you set the
168second parameter to true (boolean) it will instead return the data as a
169string.
170
171$this->load->language('file_name')
172===================================
173
174This function is an alias of the :doc:`language loading
175function <language>`: $this->lang->load()
176
177$this->load->config('file_name')
178=================================
179
180This function is an alias of the :doc:`config file loading
181function <config>`: $this->config->load()
182
183Application "Packages"
184======================
185
186An application package allows for the easy distribution of complete sets
187of resources in a single directory, complete with its own libraries,
188models, helpers, config, and language files. It is recommended that
189these packages be placed in the application/third_party folder. Below
190is a sample map of an package directory
191
192Sample Package "Foo Bar" Directory Map
193======================================
194
195The following is an example of a directory for an application package
196named "Foo Bar".
197
198::
199
200 /application/third_party/foo_bar config/ helpers/ language/ libraries/ models/
201
202Whatever the purpose of the "Foo Bar" application package, it has its
203own config files, helpers, language files, libraries, and models. To use
204these resources in your controllers, you first need to tell the Loader
205that you are going to be loading resources from a package, by adding the
206package path.
207
208$this->load->add_package_path()
209---------------------------------
210
211Adding a package path instructs the Loader class to prepend a given path
212for subsequent requests for resources. As an example, the "Foo Bar"
213application package above has a library named Foo_bar.php. In our
214controller, we'd do the following::
215
216 $this->load->add_package_path(APPPATH.'third_party/foo_bar/'); $this->load->library('foo_bar');
217
218$this->load->remove_package_path()
219------------------------------------
220
221When your controller is finished using resources from an application
222package, and particularly if you have other application packages you
223want to work with, you may wish to remove the package path so the Loader
224no longer looks in that folder for resources. To remove the last path
225added, simply call the method with no parameters.
226
227$this->load->remove_package_path()
228------------------------------------
229
230Or to remove a specific package path, specify the same path previously
231given to add_package_path() for a package.::
232
233 $this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
234
235Package view files
236------------------
237
238By Default, package view files paths are set when add_package_path()
239is called. View paths are looped through, and once a match is
240encountered that view is loaded.
241
242In this instance, it is possible for view naming collisions within
243packages to occur, and possibly the incorrect package being loaded. To
244ensure against this, set an optional second parameter of FALSE when
245calling add_package_path().
246
247::
248
249 $this->load->add_package_path(APPPATH.'my_app', FALSE);
250 $this->load->view('my_app_index'); // Loads
251 $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE
252
253 // Reset things
254 $this->load->remove_package_path(APPPATH.'my_app');
255
256 // Again without the second parameter:
257 $this->load->add_package_path(APPPATH.'my_app', TRUE);
258 $this->load->view('my_app_index'); // Loads
259 $this->load->view('welcome_message'); // Loads