Created
November 14, 2013 12:06
-
-
Save abdullahbutt/7465746 to your computer and use it in GitHub Desktop.
CI Types of Files or Classes on a CI Site .. model, library, helper, plug-in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Types of Files or Classes on a CI Site | |
| There are several different sub-folders within the application folder. We have already looked at the controller, config, and views folders. | |
| But what are libraries, models, and scripts? This is one area where CI seems rather confusing. (If you have used versions of CI before version 1.5, you'll realize why. Rick Ellis wasn't happy with the earlier versions and has changed the structure quite a lot. However, for compatibility reasons, some anomalies remain.) | |
| In a technical sense, these folders are treated in much the same way. There's no reason why you shouldn't put your code in any of these folders, though you'll have to make it slightly different in each. | |
| Let's say that you have written a block of code called display, for example, which contains a function called mainpage. There are four ways you might have done this: as a model, a library, a helper, or a plug-in. The following table summarizes the differences between each approach, and shows you how to load and use each type. | |
| model | |
| This is a class (i.e. it's object-oriented or OO code) | |
| Load it like this: $this->load->model('display'); | |
| Use it like this: $ this->display->mainpage(); | |
| Notes on syntax: | |
| It must begin with class Display extends Model | |
| It must include a constructor containing at least: | |
| function display() | |
| {parent::Model();} | |
| and contain a separate mainpage() function. | |
| Conceptually: The User Guide says, "Models are PHP classes that are designed to work with information in your database." | |
| library | |
| It is present in both the system and the application folder. Again, this is a class. (Note: your own libraries are not automatically included in the CI super-object, so you need to call CI resources in a different way. See Chapter 7 for details) | |
| Load it like this: $this->load->library('display'); | |
| Use it like this: $this->display->mainpage(); | |
| Notes on syntax: | |
| No need to extend a base class, or for a constructor function. | |
| This is enough: | |
| class Display() | |
| { | |
| function mainpage() | |
| { //code here } | |
| } | |
| Conceptually: Intended to hold your own code to extend CI functionality, or to create site-specific functionality. | |
| helper | |
| It can be in the system/helpers folder or in an application/helpers folder. This is a script (procedural code, not an OO class) | |
| Load it like this: $this->load->helper('display'); | |
| Use a function from it like this: mainpage(); | |
| Notes on syntax: | |
| The file should be saved as display_helper.php—i.e., | |
| add _helper to the file name. | |
| mainpage() should be a function included in the file, which is simply a collection of separate functions, not a class. As a result you can't directly access CI's other resources any more. | |
| Conceptually: 'helpers' are intended as a collection of low-level functions to help you perform specific tasks. | |
| plug-in | |
| It is present in the system/plugins folder but can also be created in an applications/plugins folder. This is a script (not an OO class) | |
| Load it like this: $this->load->plugin('display'); | |
| Use a function from it like this: mainpage(); | |
| Notes on syntax: | |
| The file should be saved as display_pi.php—i.e. add _pi to end | |
| of filename. | |
| mainpage() should be a function included in the file, which is simply a collection of separate functions, not a class. As a result, you can't directly access CI's other resources any more. | |
| Conceptually: The User Guide says, "…the main difference is that a plug-in usually provides a single function, whereas a Helper is usually a collection of functions….. plug-ins are intended to be created and shared by our community." (See Chapter 15 for an example plug-in.) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment