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