[CI]CodeIgniter快速開發指南,cicodeigniter_PHP教程

來源:互聯網
上載者:User

[CI]CodeIgniter快速開發指南,cicodeigniter


---------------------------------------------------------------------------------------------------------

使用CI以來最強烈的感受是其徹底的MVC設計, 舉個例子 : 在application/modesl目錄裡, 寫我們的模型操作, 統一繼承CI_Model.

而在控制器裡唯寫邏輯, 無法直接操作資料庫, 需要資料直接調用模型, 最後是調用模板.

下面分別展示模型, 控制器, 和視圖間的協作.

/** * 使用者模型, 完整CURD樣本 * @Chenwei */class User_model extends CI_model
{ public function __construct() { parent::__constrcut(); } /** * 查詢使用者資訊, 這裡不建議使用單一id參數作為條件, 為了便於控制器自己組裝條件複用此模型方法 * @param array 格式如: $where = array('id'=>1); * @return array */ public function userInfo($where = array()) { if($where && is_array($where)) { $res = $this->db->select('id, username, age')->where($where)->get('users'); return $res->result_array(); //以二維數組形式返回結果 } else {
       $res = $this->db->select('id, username, age')->get('users');
return $res->result_array(); } } /** * 添加使用者 * @param array 格式如: $data = array('username'=>'Chenwei', 'age'=>'18'); * @reteurn bool */ public function userAdd($data) { if($data && is_array($data)) { $bool = $this->db->insert('users', $data); return $bool; } else { return false; } } /** * 刪除使用者 * @param int $id * @reteurn bool */ public function userDel($id) { if($id) { $where = array('id'=>$id); $bool = $this->db->where($where)->delete('users'); return $bool; } else { return false; } } /** * 修改使用者 * @param array $where 條件 * @param array $data 新資料 * @reteurn bool */ public function userEdit($where, $data) { if($where && is_array($where)) { $bool = $this->db->where($where)->update('users', $data); return $bool; } else { return false; } }}/** * 幾點注意: * 1. 模型類名字User_model首字母大寫, 其餘字母小寫, 繼承基本模型類CI_Model * 2. 類檔案名稱 application/models/user_model.php * 3. 控制器中如何載入此模型 :
    $this->load->model('User_model', 'user'); 這是以user為對象名引入;
    $this->load->model('User_model'); 這是預設以User_model為對象名引入. 模型檔案支援子目錄;
    如果類檔案在application/models/blog/user_model.php中, 可以這樣引入: $this->load->model('blog/User_model'); * 4. 如果有需要, 你可以設定自動載入, 在 application/config/autoload.php檔案中.
* 5. 如果沒有設定自動連接資料庫, 加在模型的時候可以設定串連, 像這樣 $this->load->model('User_model', '', TRUE); */

Ps:
這裡是一個聯集查詢的例子, 有需要可以嘗試:
$res = $this->db->select('p.id, p.uid, p.order_no, p.amount, p.pay_way, p.pay_type, p.pay_bank, p.pay_time, p.goods_type, p.contact_tel, p.detail_desc, p.add_time, u.username')->from('payment as p')->join('users as u', 'p.uid = u.id')->order_by('p.id', 'desc')->get();

/** * 使用者控制器, CURD樣本 * @Chenwei */class Users extends CI_Controller{    public function __construct()    {        parent::__construct();        $this->load->model('User_model', 'user');    }        /**     * 使用者列表     */    public function index()    {        $data['user_list'] = $this->user->userInfo();        $this->load->view('user_list', $data);  //調用模板, 並將資料輸出到前台    }    /**     * 添加使用者     */    public function user_add()    {        $data = array(            'username'=>$this->input->post('name');            'age'=>intval($this->input->post('age'));        );        $bool = $this->user->userAdd($data);        if($bool)        {           $this->show_tips('操作成功 !');        }        else        {            $this->show_tips('操作失敗 !');        }    }    /**     * 修改使用者     */    public function user_edit()    {        $id = $this->input->post('id');        $data = array(            'username'=>$this->input->post('name');            'age'=>intval($this->input->post('age'));        );        if($id)        {
       $where = array('id'=>$id);
$bool = $this->user->userEdit($where, $data); if($bool) { $this->show_tips('操作成功 !'); } else { $this->show_tips('操作失敗 !'); } } else { $this->show_tips('非法操作 !'); } } /** * 刪除使用者 */ public function user_del() { $id = $this->input->post('id'); $bool = $this->user->userDel($id); if($bool) { $this->show_tips('操作成功 !'); } else { $this->show_tips('操作失敗 !'); } }}/**
* 幾點注意: * 1. 控制器檔案在 application/controller/users.php , 支援子目錄 * 2. 控制器名首字母必須大寫, 且必須繼承CI_Controller * 3. 前後台許可權控制都在application/core/MY_Controller.php檔案中,
    定義兩個控制器, 分別用於前台和後台, 繼承CI_Controller , 其餘都只需繼承這兩個自訂的控制器即可. * 4. 定義預設控制器, 在 application/config/route.php */

/** * 視圖層 樣本 * @Chenwei */php    $this->load->view('header');?>    $user_list$user_listas$v
         if():?>            foreach(  ):?>            
   endforeach;?> endif;?> 
  
$v['username'];?>
php $this->load->view('header');?>/** * 幾點注意: * 1. 模板中可以直接使用控制器中分配的變數, 使用CI系統的所有函數和方法. * 2. 開啟CI短標籤支援後, 即使php未開啟支援, CI也會幫我們自動解析, 可以放心使用. */

可能存在手誤, 以上Code不要直接複製使用; 更多CI的實用用法, 可以隨時去查閱CI手冊.

Link: http://www.cnblogs.com/farwish/p/3991419.html

@黑眼詩人 

http://www.bkjia.com/PHPjc/946584.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/946584.htmlTechArticle[CI]CodeIgniter快速開發指南,cicodeigniter --------------------------------------------------------------------------------------------------------- 使用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.