CodeIgniter study Note 3: Extended CI controller, model, codeigniterci. CodeIgniter study Note 3: Extended CI controller and model. codeigniterci 1. extended CI controllers sometimes need to perform unified operations on the controller in CI, such as logon and permission verification. 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:
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.
I. expansion of the controller in CI sometimes requires unified operations on the controller in CI, such as logon and permission verification...