CodeIgniter study Note 3: Extended CI controller and model, codeigniterci_PHP tutorial

Source: Internet
Author: User
Tags codeigniter
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.