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>`, |
dchill42 | 3169f26 | 2012-08-11 20:12:05 -0400 | [diff] [blame] | 7 | :doc:`Drivers <../general/drivers>`, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 8 | :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 Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 14 | .. contents:: |
| 15 | :local: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 16 | |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 17 | .. raw:: html |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 19 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | |
| 21 | Application "Packages" |
| 22 | ====================== |
| 23 | |
| 24 | An application package allows for the easy distribution of complete sets |
| 25 | of resources in a single directory, complete with its own libraries, |
| 26 | models, helpers, config, and language files. It is recommended that |
Andrey Andreev | c26d34f | 2013-01-28 21:46:08 +0200 | [diff] [blame] | 27 | these packages be placed in the application/third_party directory. Below |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 28 | is a sample map of an package directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 29 | |
| 30 | The following is an example of a directory for an application package |
| 31 | named "Foo Bar". |
| 32 | |
| 33 | :: |
| 34 | |
Derek Jones | 36be969 | 2011-10-05 15:52:41 -0500 | [diff] [blame] | 35 | /application/third_party/foo_bar |
| 36 | |
| 37 | config/ |
| 38 | helpers/ |
| 39 | language/ |
| 40 | libraries/ |
| 41 | models/ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
| 43 | Whatever the purpose of the "Foo Bar" application package, it has its |
| 44 | own config files, helpers, language files, libraries, and models. To use |
| 45 | these resources in your controllers, you first need to tell the Loader |
| 46 | that you are going to be loading resources from a package, by adding the |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 47 | package path via the ``add_package_path()`` method. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | |
| 49 | Package view files |
| 50 | ------------------ |
| 51 | |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 52 | By Default, package view files paths are set when ``add_package_path()`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | is called. View paths are looped through, and once a match is |
| 54 | encountered that view is loaded. |
| 55 | |
| 56 | In this instance, it is possible for view naming collisions within |
| 57 | packages to occur, and possibly the incorrect package being loaded. To |
| 58 | ensure against this, set an optional second parameter of FALSE when |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 59 | calling ``add_package_path()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | |
| 61 | :: |
| 62 | |
| 63 | $this->load->add_package_path(APPPATH.'my_app', FALSE); |
| 64 | $this->load->view('my_app_index'); // Loads |
| 65 | $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE |
| 66 | |
| 67 | // Reset things |
| 68 | $this->load->remove_package_path(APPPATH.'my_app'); |
| 69 | |
| 70 | // Again without the second parameter: |
W. Kristianto | a5e329f | 2012-09-23 22:34:18 +0700 | [diff] [blame] | 71 | $this->load->add_package_path(APPPATH.'my_app'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | $this->load->view('my_app_index'); // Loads |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 73 | $this->load->view('welcome_message'); // Loads |
| 74 | |
| 75 | *************** |
| 76 | Class Reference |
| 77 | *************** |
| 78 | |
| 79 | .. class:: CI_Loader |
| 80 | |
| 81 | .. method:: library($library[, $params = NULL[, $object_name = NULL]]) |
| 82 | |
| 83 | :param mixed $library: Library name as a string or an array with multiple libraries |
| 84 | :param array $params: Optional array of parameters to pass to the loaded library's constructor |
| 85 | :param string $object_name: Optional object name to assign the library to |
Andrey Andreev | 0c9d080 | 2014-01-07 13:39:23 +0200 | [diff] [blame] | 86 | :returns: object |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 87 | |
| 88 | This method is used to load core classes. |
| 89 | |
| 90 | .. note:: We use the terms "class" and "library" interchangeably. |
| 91 | |
| 92 | For example, if you would like to send email with CodeIgniter, the first |
| 93 | step is to load the email class within your controller:: |
| 94 | |
| 95 | $this->load->library('email'); |
| 96 | |
| 97 | Once loaded, the library will be ready for use, using ``$this->email``. |
| 98 | |
| 99 | Library files can be stored in subdirectories within the main |
| 100 | "libraries" directory, or within your personal *application/libraries* |
| 101 | directory. To load a file located in a subdirectory, simply include the |
| 102 | path, relative to the "libraries" directory. For example, if you have |
| 103 | file located at:: |
| 104 | |
| 105 | libraries/flavors/Chocolate.php |
| 106 | |
| 107 | You will load it using:: |
| 108 | |
| 109 | $this->load->library('flavors/chocolate'); |
| 110 | |
| 111 | You may nest the file in as many subdirectories as you want. |
| 112 | |
| 113 | Additionally, multiple libraries can be loaded at the same time by |
| 114 | passing an array of libraries to the load method. |
| 115 | :: |
| 116 | |
| 117 | $this->load->library(array('email', 'table')); |
| 118 | |
Connor Tumbleson | 75b3fb2 | 2014-01-11 06:58:43 -0600 | [diff] [blame^] | 119 | **Setting options** |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 120 | |
| 121 | The second (optional) parameter allows you to optionally pass |
| 122 | configuration setting. You will typically pass these as an array:: |
| 123 | |
| 124 | $config = array ( |
| 125 | 'mailtype' => 'html', |
| 126 | 'charset' => 'utf-8, |
| 127 | 'priority' => '1' |
| 128 | ); |
| 129 | |
| 130 | $this->load->library('email', $config); |
| 131 | |
| 132 | Config options can usually also be set via a config file. Each library |
| 133 | is explained in detail in its own page, so please read the information |
| 134 | regarding each one you would like to use. |
| 135 | |
| 136 | Please take note, when multiple libraries are supplied in an array for |
| 137 | the first parameter, each will receive the same parameter information. |
| 138 | |
Connor Tumbleson | 75b3fb2 | 2014-01-11 06:58:43 -0600 | [diff] [blame^] | 139 | **Assigning a Library to a different object name** |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 140 | |
| 141 | If the third (optional) parameter is blank, the library will usually be |
| 142 | assigned to an object with the same name as the library. For example, if |
| 143 | the library is named Calendar, it will be assigned to a variable named |
| 144 | ``$this->calendar``. |
| 145 | |
| 146 | If you prefer to set your own class names you can pass its value to the |
| 147 | third parameter:: |
| 148 | |
| 149 | $this->load->library('calendar', NULL, 'my_calendar'); |
| 150 | |
| 151 | // Calendar class is now accessed using: |
| 152 | $this->my_calendar |
| 153 | |
| 154 | Please take note, when multiple libraries are supplied in an array for |
| 155 | the first parameter, this parameter is discarded. |
| 156 | |
| 157 | .. method:: driver($library[, $params = NULL[, $object_name]]) |
| 158 | |
| 159 | :param mixed $library: Library name as a string or an array with multiple libraries |
| 160 | :param array $params: Optional array of parameters to pass to the loaded library's constructor |
| 161 | :param string $object_name: Optional object name to assign the library to |
Andrey Andreev | 0c9d080 | 2014-01-07 13:39:23 +0200 | [diff] [blame] | 162 | :returns: object |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 163 | |
| 164 | This method is used to load driver libraries, acts very much like the |
| 165 | ``library()`` method. |
| 166 | |
| 167 | As an example, if you would like to use sessions with CodeIgniter, the first |
| 168 | step is to load the session driver within your controller:: |
| 169 | |
| 170 | $this->load->driver('session'); |
| 171 | |
| 172 | Once loaded, the library will be ready for use, using ``$this->session``. |
| 173 | |
| 174 | Driver files must be stored in a subdirectory within the main |
| 175 | "libraries" directory, or within your personal *application/libraries* |
| 176 | directory. The subdirectory must match the parent class name. Read the |
| 177 | :doc:`Drivers <../general/drivers>` description for details. |
| 178 | |
| 179 | Additionally, multiple driver libraries can be loaded at the same time by |
| 180 | passing an array of drivers to the load method. |
| 181 | :: |
| 182 | |
| 183 | $this->load->driver(array('session', 'cache')); |
| 184 | |
Connor Tumbleson | 75b3fb2 | 2014-01-11 06:58:43 -0600 | [diff] [blame^] | 185 | **Setting options** |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 186 | |
| 187 | The second (optional) parameter allows you to optionally pass |
| 188 | configuration settings. You will typically pass these as an array:: |
| 189 | |
| 190 | $config = array( |
| 191 | 'sess_driver' => 'cookie', |
| 192 | 'sess_encrypt_cookie' => true, |
| 193 | 'encryption_key' => 'mysecretkey' |
| 194 | ); |
| 195 | |
| 196 | $this->load->driver('session', $config); |
| 197 | |
| 198 | Config options can usually also be set via a config file. Each library |
| 199 | is explained in detail in its own page, so please read the information |
| 200 | regarding each one you would like to use. |
| 201 | |
Connor Tumbleson | 75b3fb2 | 2014-01-11 06:58:43 -0600 | [diff] [blame^] | 202 | **Assigning a Driver to a different object name** |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 203 | |
| 204 | If the third (optional) parameter is blank, the library will be assigned |
| 205 | to an object with the same name as the parent class. For example, if |
| 206 | the library is named Session, it will be assigned to a variable named |
| 207 | ``$this->session``. |
| 208 | |
| 209 | If you prefer to set your own class names you can pass its value to the |
| 210 | third parameter:: |
| 211 | |
| 212 | $this->load->library('session', '', 'my_session'); |
| 213 | |
| 214 | // Session class is now accessed using: |
| 215 | $this->my_session |
| 216 | |
| 217 | .. method:: view($view[, $vars = array()[, return = FALSE]]) |
| 218 | |
| 219 | :param string $view: View name |
| 220 | :param array $vars: An associative array of variables |
| 221 | :param bool $return: Whether to return the loaded view |
| 222 | :returns: mixed |
| 223 | |
| 224 | This method is used to load your View files. If you haven't read the |
| 225 | :doc:`Views <../general/views>` section of the user guide it is |
| 226 | recommended that you do since it shows you how this method is |
| 227 | typically used. |
| 228 | |
| 229 | The first parameter is required. It is the name of the view file you |
| 230 | would like to load. |
| 231 | |
| 232 | .. note:: The .php file extension does not need to be specified unless |
| 233 | you use something other than .php. |
| 234 | |
| 235 | The second **optional** parameter can take an associative array or an |
| 236 | object as input, which it runs through the PHP |
| 237 | `extract() <http://www.php.net/extract>`_ function to convert to variables |
| 238 | that can be used in your view files. Again, read the |
| 239 | :doc:`Views <../general/views>` page to learn how this might be useful. |
| 240 | |
| 241 | The third **optional** parameter lets you change the behavior of the |
| 242 | method so that it returns data as a string rather than sending it to |
| 243 | your browser. This can be useful if you want to process the data in some |
| 244 | way. If you set the parameter to TRUE (boolean) it will return data. The |
| 245 | default behavior is FALSE, which sends it to your browser. Remember to |
| 246 | assign it to a variable if you want the data returned:: |
| 247 | |
| 248 | $string = $this->load->view('myfile', '', TRUE); |
| 249 | |
| 250 | .. method:: vars($vars[, $val = '']) |
| 251 | |
| 252 | :param mixed $vars: An array of variables or a single variable name |
| 253 | :param mixed $val: Optional variable value |
Andrey Andreev | 0c9d080 | 2014-01-07 13:39:23 +0200 | [diff] [blame] | 254 | :returns: object |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 255 | |
| 256 | This method takes an associative array as input and generates |
| 257 | variables using the PHP `extract() <http://www.php.net/extract>`_ |
| 258 | function. This method produces the same result as using the second |
| 259 | parameter of the ``$this->load->view()`` method above. The reason you |
| 260 | might want to use this method independently is if you would like to |
| 261 | set some global variables in the constructor of your controller and have |
| 262 | them become available in any view file loaded from any method. You can |
| 263 | have multiple calls to this method. The data get cached and merged |
| 264 | into one array for conversion to variables. |
| 265 | |
| 266 | .. method:: get_var($key) |
| 267 | |
| 268 | :param string $key: Variable name key |
| 269 | :returns: mixed |
| 270 | |
| 271 | This method checks the associative array of variables available to |
| 272 | your views. This is useful if for any reason a var is set in a library |
| 273 | or another controller method using ``$this->load->vars()``. |
| 274 | |
| 275 | .. method:: get_vars() |
| 276 | |
| 277 | :returns: array |
| 278 | |
| 279 | This method retrieves all variables available to your views. |
| 280 | |
| 281 | .. method:: model($model[, $name = ''[, $db_conn = FALSE]]) |
| 282 | |
| 283 | :param mixed $model: Model name or an array containing multiple models |
| 284 | :param string $name: Optional object name to assign the model to |
| 285 | :param string $db_conn: Optional database configuration group to load |
Andrey Andreev | 0c9d080 | 2014-01-07 13:39:23 +0200 | [diff] [blame] | 286 | :returns: object |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 287 | |
| 288 | :: |
| 289 | |
| 290 | $this->load->model('model_name'); |
| 291 | |
| 292 | |
| 293 | If your model is located in a subdirectory, include the relative path |
| 294 | from your models directory. For example, if you have a model located at |
| 295 | *application/models/blog/queries.php* you'll load it using:: |
| 296 | |
| 297 | $this->load->model('blog/queries'); |
| 298 | |
| 299 | If you would like your model assigned to a different object name you can |
| 300 | specify it via the second parameter of the loading method:: |
| 301 | |
| 302 | $this->load->model('model_name', 'fubar'); |
| 303 | $this->fubar->method(); |
| 304 | |
| 305 | .. method:: database([$params = ''[, $return = FALSE[, $query_builder = NULL]]]) |
| 306 | |
| 307 | :param mixed $params: Database group name or configuration options |
| 308 | :param bool $return: Whether to return the loaded database object |
| 309 | :param bool $query_builder: Whether to load the Query Builder |
| 310 | :returns: mixed |
| 311 | |
| 312 | This method lets you load the database class. The two parameters are |
| 313 | **optional**. Please see the :doc:`database <../database/index>` |
| 314 | section for more info. |
| 315 | |
| 316 | .. method:: dbforge([$db = NULL[, $return = FALSE]]) |
| 317 | |
| 318 | :param object $db: Database object |
| 319 | :param bool $return: Whether to return the Database Forge instance |
| 320 | :returns: mixed |
| 321 | |
| 322 | Loads the :doc:`Database Forge <../database/forge>` class, please refer |
| 323 | to that manual for more info. |
| 324 | |
| 325 | .. method:: dbutil([$db = NULL[, $return = FALSE]]) |
| 326 | |
| 327 | :param object $db: Database object |
| 328 | :param bool $return: Whether to return the Database Utilities instance |
| 329 | :returns: mixed |
| 330 | |
| 331 | Loads the :doc:`Database Utilities <../database/utilities>` class, please |
| 332 | refer to that manual for more info. |
| 333 | |
| 334 | .. method:: helper($helpers) |
| 335 | |
| 336 | :param mixed $helpers: Helper name as a string or an array containing multiple helpers |
Andrey Andreev | 0c9d080 | 2014-01-07 13:39:23 +0200 | [diff] [blame] | 337 | :returns: object |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 338 | |
| 339 | This method loads helper files, where file_name is the name of the |
| 340 | file, without the _helper.php extension. |
| 341 | |
| 342 | .. method:: file($path[, $return = FALSE]) |
| 343 | |
| 344 | :param string $path: File path |
| 345 | :param bool $return: Whether to return the loaded file |
| 346 | :returns: mixed |
| 347 | |
| 348 | This is a generic file loading method. Supply the filepath and name in |
| 349 | the first parameter and it will open and read the file. By default the |
| 350 | data is sent to your browser, just like a View file, but if you set the |
| 351 | second parameter to boolean TRUE it will instead return the data as a |
| 352 | string. |
| 353 | |
| 354 | .. method:: language($files[, $lang = '']) |
| 355 | |
| 356 | :param mixed $files: Language file name or an array of multiple language files |
| 357 | :param string $lang: Language name |
Andrey Andreev | 0c9d080 | 2014-01-07 13:39:23 +0200 | [diff] [blame] | 358 | :returns: object |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 359 | |
| 360 | This method is an alias of the :doc:`language loading |
| 361 | method <language>`: ``$this->lang->load()``. |
| 362 | |
| 363 | .. method:: config($file[, $use_sections = FALSE[, $fail_gracefully = FALSE]]) |
| 364 | |
| 365 | :param string $file: Configuration file name |
| 366 | :param bool $use_sections: Whether configuration values should be loaded into their own section |
| 367 | :param bool $fail_gracefully: Whether to just return FALSE in case of failure |
| 368 | :returns: bool |
| 369 | |
| 370 | This method is an alias of the :doc:`config file loading |
| 371 | method <config>`: ``$this->config->load()`` |
| 372 | |
| 373 | .. method:: add_package_path($path[, $view_cascade = TRUE]) |
| 374 | |
| 375 | :param string $path: Path to add |
| 376 | :param bool $view_cascade: Whether to use cascading views |
Andrey Andreev | 0c9d080 | 2014-01-07 13:39:23 +0200 | [diff] [blame] | 377 | :returns: object |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 378 | |
| 379 | Adding a package path instructs the Loader class to prepend a given path |
| 380 | for subsequent requests for resources. As an example, the "Foo Bar" |
| 381 | application package above has a library named Foo_bar.php. In our |
| 382 | controller, we'd do the following:: |
| 383 | |
Andrey Andreev | 0c9d080 | 2014-01-07 13:39:23 +0200 | [diff] [blame] | 384 | $this->load->add_package_path(APPPATH.'third_party/foo_bar/') |
| 385 | ->library('foo_bar'); |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 386 | |
| 387 | .. method:: remove_package_path([$path = '']) |
| 388 | |
| 389 | :param string $path: Path to remove |
Andrey Andreev | 0c9d080 | 2014-01-07 13:39:23 +0200 | [diff] [blame] | 390 | :returns: object |
Andrey Andreev | 0556396 | 2014-01-06 12:28:59 +0200 | [diff] [blame] | 391 | |
| 392 | When your controller is finished using resources from an application |
| 393 | package, and particularly if you have other application packages you |
| 394 | want to work with, you may wish to remove the package path so the Loader |
| 395 | no longer looks in that directory for resources. To remove the last path |
| 396 | added, simply call the method with no parameters. |
| 397 | |
| 398 | Or to remove a specific package path, specify the same path previously |
| 399 | given to ``add_package_path()`` for a package.:: |
| 400 | |
| 401 | $this->load->remove_package_path(APPPATH.'third_party/foo_bar/'); |
| 402 | |
| 403 | .. method:: get_package_paths([$include_base = TRUE]) |
| 404 | |
| 405 | :param bool $include_base: Whether to include BASEPATH |
| 406 | :returns: array |
| 407 | |
| 408 | Returns all currently available package paths. |