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