CodeIgniter学习笔记三:扩展CI的控制器、模型

一、扩展CI中的控制器

有时需要对CI中的控制器作统一操作,如进行登录和权限验证,这时就可以通过扩展CI控制器来实现。

扩展CI控制器只需要在application/core文件夹中建一个继承自CI_Controller类的MY_Controller类即可,然后在这个类中实现自己需要的逻辑。

关于上面这句话,有两点需要解释一下:

1、为什么要在application/core文件夹中:是因为基类CI_Controller是在system/core文件夹中,这里需要跟system中对应。

2、为什么扩展的控制器前缀是MY_,可否换成其他的:这个前缀是在application/config/config.php中定义的:

$config['subclass_prefix'] = 'MY_';

只需要这两处对应上就可以了。

二、模型

示例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();
        }
    }

注意点:

1、文件名全小写

2、类名首字母大写

3、模型中可以使用超级对象中的属性

4、建议用_model作后缀,防止跟其他类名冲突

使用示例:

public function index() {
    //load model
    $this -> load -> model('User_model');
    $usermodel = $this -> User_model -> getAll();

    //别名
    $this -> load -> model('User_model', 'user');
    $usermodel = $this -> user -> getAll();
    var_dump($usermodel);
}

模型主要用于规范项目结构。


本文转载自:http://www.cnblogs.com/lurenjiashuo/p/ci-note-basic-3-customer-controller-models.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值