CodeIgniter學習筆記三:擴充CI的控制器、模型,codeigniterci_PHP教程

來源:互聯網
上載者:User

CodeIgniter學習筆記三:擴充CI的控制器、模型,codeigniterci


一、擴充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.bkjia.com/PHPjc/1014181.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1014181.htmlTechArticleCodeIgniter學習筆記三:擴充CI的控制器、模型,codeigniterci 一、擴充CI中的控制器 有時需要對CI中的控制器作統一操作,如進行登入和許可權驗...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.