General
- Class names must have the first letter capitalized with the rest of the name lowercase.
ex : class Model_name extends Model {} - The file name will be a lower case version of your class name.
ex : application/models/user_model.php
Controller
- A Controller is simply a class file that is named in a way that can be associated with a URI
- If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.
- To make a function private, simply add an underscore as the name prefix and it will not be served via a URL request. ex: function _utility()
Views
- Views are never called directly, they must be loaded by a controller.
- $this->load->view('folder_name/file_name');
- There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser.
ex : $string = $this->load->view('myfile', '', true);
Models
- Models are PHP classes that are designed to work with information in your database.
- Your models will typically be loaded and called from within your controller functions.
- Once loaded, you will access your model functions using an object with the same name as your class:
$this->load->model('Model_name');
$this->Model_name->function(); - If you would like your model assigned to a different object name you can specify it via the second parameter of the loading function:
$this->load->model('Model_name', 'fubar');
$this->fubar->function(); - When a model is loaded it does NOT connect automatically to your database.
- You can tell the model loading function to auto-connect by passing TRUE (boolean) via the third parameter, and connectivity settings, as defined in your database config file will be used:
$this->load->model('Model_name', '', TRUE);
Helper Functions
- Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.
- CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.
- Loading Multiple Helpers. ex: $this->load->helper( array('helper1', 'helper2', 'helper3') );
- all native CodeIgniter libraries are prefixed with CI_ so DO NOT use that as your prefix.
Plugins
- Plugins work almost identically to Helpers. The main difference is that a plugin usually provides a single function, whereas a Helper is usually a collection of functions.
- Loading Multiple Plugins. ex: $this->load->plugin( array('plugin1', 'plugin2', 'plugin3') );
Common Functions
- is_really_writable('path/to/file')
- config_item('item_key')
- show_error('message'), show_404('page'), log_message('level', 'message')
Scaffolding
- Scaffolding is intended for development use only.
- $this->load->scaffolding('table_name');
- example.com/index.php/class/secret_word/
URI Routing
- example.com/class/function/id/
- example.com/product/1/ => $route['product/(:num)'] = "catalog/product_lookup_by_id/$1"
- $route['product/:any'] = "catalog/product_lookup";
Error Handling
- Disabling error reporting will NOT prevent log files from being written if there are errors.
沒有留言:
張貼留言