CodeIgniter study Note 3: Extended CI controller and model, codeigniterci
1. Extend the Controller in CI
Sometimes it is necessary to perform unified operations on the controller in the CI, such as logon and permission verification, then it can be achieved by extending the CI controller.
To expand the CI controller, you only need to create a MY_Controller class inherited from the CI_Controller class in the application/core folder, and then implement the logic you need in this class.
There are two points to explain about the above sentence:
1. Why is it in the application/core Folder: Because the base class CI_Controller is in the system/core folder, it must correspond to the system.
2. Why is the prefix of the extended controller MY _ changed to another one? The prefix is defined in application/config. php:
$config['subclass_prefix'] = 'MY_';
You only need to match the two locations.
Ii. Model
Example application/models/user_model.php:
<?php /** * User_model */ class User_model extends CI_Model{ //return all users public function getAll() { $res = $this -> db -> get('test'); return $res -> result(); } }
Note:
1. All file names are in lowercase.
2. The first letter of the class name is capitalized.
3. attributes in super objects can be used in the model.
4. We recommend that you use _ model as the suffix to prevent conflicts with other class names.
Example:
Public function index () {// load model $ this-> load-> model ('user _ model'); $ usermodel = $ this-> User_model-> getAll (); // alias $ this-> load-> model ('user _ model', 'user'); $ usermodel = $ this-> User-> getAll (); var_dump ($ usermodel );}
The model is mainly used to standardize the project structure.