############ Loader Class ############ Loader, as the name suggests, is used to load elements. These elements can be libraries (classes) :doc:`View files <../general/views>`, :doc:`Drivers <../general/drivers>`, :doc:`Helpers <../general/helpers>`, :doc:`Models <../general/models>`, or your own files. .. note:: This class is initialized automatically by the system so there is no need to do it manually. .. contents:: :local: .. raw:: html
Application "Packages" ====================== An application package allows for the easy distribution of complete sets of resources in a single directory, complete with its own libraries, models, helpers, config, and language files. It is recommended that these packages be placed in the application/third_party directory. Below is a sample map of an package directory. The following is an example of a directory for an application package named "Foo Bar". :: /application/third_party/foo_bar config/ helpers/ language/ libraries/ models/ Whatever the purpose of the "Foo Bar" application package, it has its own config files, helpers, language files, libraries, and models. To use these resources in your controllers, you first need to tell the Loader that you are going to be loading resources from a package, by adding the package path via the ``add_package_path()`` method. Package view files ------------------ By Default, package view files paths are set when ``add_package_path()`` is called. View paths are looped through, and once a match is encountered that view is loaded. In this instance, it is possible for view naming collisions within packages to occur, and possibly the incorrect package being loaded. To ensure against this, set an optional second parameter of FALSE when calling ``add_package_path()``. :: $this->load->add_package_path(APPPATH.'my_app', FALSE); $this->load->view('my_app_index'); // Loads $this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is FALSE // Reset things $this->load->remove_package_path(APPPATH.'my_app'); // Again without the second parameter: $this->load->add_package_path(APPPATH.'my_app'); $this->load->view('my_app_index'); // Loads $this->load->view('welcome_message'); // Loads *************** Class Reference *************** .. class:: CI_Loader .. method:: library($library[, $params = NULL[, $object_name = NULL]]) :param mixed $library: Library name as a string or an array with multiple libraries :param array $params: Optional array of parameters to pass to the loaded library's constructor :param string $object_name: Optional object name to assign the library to :returns: object This method is used to load core classes. .. note:: We use the terms "class" and "library" interchangeably. For example, if you would like to send email with CodeIgniter, the first step is to load the email class within your controller:: $this->load->library('email'); Once loaded, the library will be ready for use, using ``$this->email``. Library files can be stored in subdirectories within the main "libraries" directory, or within your personal *application/libraries* directory. To load a file located in a subdirectory, simply include the path, relative to the "libraries" directory. For example, if you have file located at:: libraries/flavors/Chocolate.php You will load it using:: $this->load->library('flavors/chocolate'); You may nest the file in as many subdirectories as you want. Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load method. :: $this->load->library(array('email', 'table')); **Setting options** The second (optional) parameter allows you to optionally pass configuration setting. You will typically pass these as an array:: $config = array ( 'mailtype' => 'html', 'charset' => 'utf-8, 'priority' => '1' ); $this->load->library('email', $config); Config options can usually also be set via a config file. Each library is explained in detail in its own page, so please read the information regarding each one you would like to use. Please take note, when multiple libraries are supplied in an array for the first parameter, each will receive the same parameter information. **Assigning a Library to a different object name** If the third (optional) parameter is blank, the library will usually be assigned to an object with the same name as the library. For example, if the library is named Calendar, it will be assigned to a variable named ``$this->calendar``. If you prefer to set your own class names you can pass its value to the third parameter:: $this->load->library('calendar', NULL, 'my_calendar'); // Calendar class is now accessed using: $this->my_calendar Please take note, when multiple libraries are supplied in an array for the first parameter, this parameter is discarded. .. method:: driver($library[, $params = NULL[, $object_name]]) :param mixed $library: Library name as a string or an array with multiple libraries :param array $params: Optional array of parameters to pass to the loaded library's constructor :param string $object_name: Optional object name to assign the library to :returns: object This method is used to load driver libraries, acts very much like the ``library()`` method. As an example, if you would like to use sessions with CodeIgniter, the first step is to load the session driver within your controller:: $this->load->driver('session'); Once loaded, the library will be ready for use, using ``$this->session``. Driver files must be stored in a subdirectory within the main "libraries" directory, or within your personal *application/libraries* directory. The subdirectory must match the parent class name. Read the :doc:`Drivers <../general/drivers>` description for details. Additionally, multiple driver libraries can be loaded at the same time by passing an array of drivers to the load method. :: $this->load->driver(array('session', 'cache')); **Setting options** The second (optional) parameter allows you to optionally pass configuration settings. You will typically pass these as an array:: $config = array( 'sess_driver' => 'cookie', 'sess_encrypt_cookie' => true, 'encryption_key' => 'mysecretkey' ); $this->load->driver('session', $config); Config options can usually also be set via a config file. Each library is explained in detail in its own page, so please read the information regarding each one you would like to use. **Assigning a Driver to a different object name** If the third (optional) parameter is blank, the library will be assigned to an object with the same name as the parent class. For example, if the library is named Session, it will be assigned to a variable named ``$this->session``. If you prefer to set your own class names you can pass its value to the third parameter:: $this->load->library('session', '', 'my_session'); // Session class is now accessed using: $this->my_session .. method:: view($view[, $vars = array()[, return = FALSE]]) :param string $view: View name :param array $vars: An associative array of variables :param bool $return: Whether to return the loaded view :returns: mixed This method is used to load your View files. If you haven't read the :doc:`Views <../general/views>` section of the user guide it is recommended that you do since it shows you how this method is typically used. The first parameter is required. It is the name of the view file you would like to load. .. note:: The .php file extension does not need to be specified unless you use something other than .php. The second **optional** parameter can take an associative array or an object as input, which it runs through the PHP `extract()