CodeIgniter study Note 3: Extended CI controller and model, codeigniterci

Source: Internet
Author: User
Tags codeigniter

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.

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.