Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CodeIgniter Library and extend Core Class

Bo-Yi Wu
February 23, 2012

CodeIgniter Library and extend Core Class

how to make best use of codeigniter library and extend core class
http://blog.wu-boy.com/2012/02/how-to-make-best-use-of-codeigniter-library-and-extend-core-class/

Bo-Yi Wu

February 23, 2012
Tweet

More Decks by Bo-Yi Wu

Other Decks in Programming

Transcript

  1. 8 public function __construct($config = array()) { if (count($config) >

    0) { $this->initialize($config); } } Library 參數初始化 initialize 為 class 其中一個 function
  2. 16 application/config/bitly.php application/config/tiny.php <?php $config['user_name'] = 'appleboy'; $config['api_key'] = 'api_key_01';

    $config['format'] = 'json'; ?> <?php $config['user_name'] = 'appleboy46'; $config['api_key'] = 'api_key_02'; $config['format'] = 'xml'; ?>
  3. 26 application/config/bitly.php application/config/tiny.php <?php $config['bitly_user_name'] = 'appleboy'; $config['bitly_api_key'] = 'api_key_01';

    $config['bitly_format'] = 'json'; ?> <?php $config['tiny_user_name'] = 'appleboy46'; $config['tiny_api_key'] = 'api_key_02'; $config['tiny_format'] = 'xml'; ?>
  4. 28 作業一  新增 template 設定檔  base_title  site_keyword

     site_description  將設定檔內容讀出並載入到 layout
  5. 30 Email Class  多重收件人  副本 (CC) 和密件副本 (BCCs)

     支援 HTML 或者是純文字 (Plaintext) 郵件  附件檔案  Email Debugging tools
  6. 32  $this­>email­>to()  $this­>email­>cc()  $this­>email­>bcc()  可以帶入陣列或單一資料 

    $this­>email­>send() if ( ! $this->email->send()) { echo $this->email->print_debugger(); }
  7. 35 檔案上傳流程  設定存放上傳目錄權限  調整 php.ini 檔案上傳設定  建立上傳表單

     驗證使用者上傳檔案  印出檔案訊息並且寫到資料庫
  8. 36 $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100';

    $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); if ( ! $this->upload->do_upload('input_column_name')) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form' , $error); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success' , $data); }
  9. 37 設定參數  upload_path( 絕對或者是相對即可 )  allowed_types( 可上傳類型用豎線 '|'

    分開 )  overwrite( 相同檔名則覆蓋 )  max_size( 檔案大小限制 )  max_width( 圖片專用 )  max_height( 圖片專用 )  encrypt_name( 檔名加密字串取代 )
  10. 38 $this->upload->data() [file_name] => mypic.jpg [file_type] => image/jpeg [file_path] =>

    /path/to/your/upload/ [full_path] => /path/to/your/upload/jpg.jpg [raw_name] => mypic [orig_name] => mypic.jpg [file_ext] => .jpg [file_size] => 22.2 [is_image] => 1 [image_width] => 800 [image_height] => 600 [image_type] => jpeg [image_size_str] => width="800" height="200"
  11. 44 驗證規則 ( 註冊表單 ) $this->form_validation->set_rules('username', ' 帳號 ', 'required');

    $this->form_validation->set_rules('password', ' 密碼 ', 'required'); $this->form_validation->set_rules('passconf', ' 確認 ', 'required'); $this->form_validation->set_rules('email', ' 電子郵件 ', 'required');
  12. 45 Form Controller $this->load->library('form_validation'); $this->form_validation->set_rules('username', ' 帳號 ', 'required'); $this->form_validation->set_rules('password',

    ' 密碼 ', 'required'); $this->form_validation->set_rules('passconf', ' 確認密碼 ', 'required'); $this->form_validation->set_rules('email', ' 電子郵件 ', 'required'); if ($this->form_validation->run() == FALSE) { $this->load->view('myform'); } else { $this->load->view('formsuccess'); }
  13. 46 多重驗證規則 $this->form_validation->set_rules('username', ' 帳號 ', 'required|min_length[5]|max_length[12]| is_unique[users.username]'); $this->form_validation->set_rules('password', '

    密碼 ', 'required|matches[passconf]'); $this->form_validation->set_rules('passconf', ' 確認密碼 ', 'required'); $this->form_validation->set_rules('email', ' 電子郵件 ', 'required|valid_email|is_unique[users.email]');
  14. 47 事先處理欄位 $this->form_validation->set_rules('username', ' 帳號 ', 'trim| required|min_length[5]|max_length[12]|xss_clean'); $this->form_validation->set_rules('password', '

    密碼 ', 'trim| required|matches[passconf]|md5'); $this->form_validation->set_rules('passconf', ' 確認密碼 ', 'trim|required'); $this->form_validation->set_rules('email', ' 電子郵件 ', 'trim| required|valid_email|is_unique[users.email]'); htmlspecialchars, trim, MD5(native function) 皆可處理
  15. 50 public function index() { $this->load->library('form_validation'); $this->form_validation->set_rules('username', ' 帳號 ',

    'callback_username_check'); $this->form_validation->set_rules('password', ' 密碼 ', 'required'); $this->form_validation->set_rules('passconf', ' 確認密碼 ', 'required'); $this->form_validation->set_rules('email', ' 電子郵件 ', 'required'); if ($this->form_validation->run() == FALSE) { $this->load->view('myform'); } else { $this->load->view('formsuccess'); } }
  16. 51 Call back function public function username_check($str) { if ($str

    == 'test') { return FALSE; } else { return TRUE; } }
  17. 54 作業三  使用者註冊表單  欄位 :Email, 密碼 , 確認密碼

     撰寫 call back function 確認帳號是否存在  自訂錯誤訊息
  18. 57 個人建議大量圖形網站用 ImageMagic ( 支援 command line 執行 ) (

    獨立處理圖片 ) ( 不需要動到前端 Server)
  19. 60 處理影像 $config['image_library'] = 'gd2'; $config['source_image'] = '/path/mypic.jpg'; $config['create_thumb'] =

    true; $config['width'] = 75; $config['height'] = 50; $this->load->library('image_lib', $config); $this->image_lib->resize();
  20. 61 參數設定  image_library: GD, GD2, ImageMagick, NetPBM  library_path:

    Linux, Windows 底下路徑  source_image: 絕對 / 相對路徑  new_image: 輸出檔案絕對 / 相對路徑  width: 圖片寬度  height: 圖片高度  create_thumb: 避免覆蓋原檔案
  21. 67 $config['image_library'] = 'imagemagick'; $config['library_path'] = '/usr/local/bin'; $config['source_image'] = '/path/to/mypic.jpg';

    $config['x_axis'] = '100'; $config['y_axis'] = '60'; $this->image_lib->initialize($config); if ( ! $this->image_lib->crop()) { echo $this->image_lib->display_errors(); }
  22. 69 $config['image_library'] = 'netpbm'; $config['library_path'] = '/usr/bin/'; $config['source_image'] = '/path/to/mypic.jpg';

    $config['rotation_angle'] = 'hor'; $this->image_lib->initialize($config); if ( ! $this->image_lib->rotate()) { echo $this->image_lib->display_errors(); }
  23. 70 rotation_angle 參數  90( 順時針 90 度 ) 

    180( 順時針 180 度 )  270( 順時針 270 度 )  hor( 水平旋轉 )  vrt( 垂直旋轉 )
  24. 77 $lang['error_email'] = " 您必須填入電子郵件 "; $lang['error_url'] = " 您必須填入完整網址

    "; $lang['error_username'] = " 您必須填入帳號名稱 "; 避免衝突
  25. 83 分頁參數  base_url: 完整的 URL 路徑 , 包含了控制器 (controller)/

    函數 (function)  total_rows: 總筆數  per_page: 單頁顯示幾筆  uri_segment: URI 哪個部份包含了頁數  num_links: 您目前所在頁數前面跟後面所顯示的 分頁數量  use_page_numbers: 用 page number 顯示
  26. 86 注意事項  檔案命名  一律採用小寫命名 , 如 my_class.php 

    類別宣告  類別宣告第一個字母必須為大寫 , 如 class My_class
  27. 87 <?php if ( ! defined('BASEPATH')) exit('No direct script access

    allowed'); class Someclass { public function some_function() { } } /* End of file Someclass.php */
  28. 89 public function __construct($config = array()) { If ( !

    empty($config)) { $this->initialize($config); } }
  29. 94 作業  建立個人專用 Library  將 goo.gl php library

    導入  http://code.google.com/p/googl­php/
  30. 98 class Welcome_01 extends CI_Controller class Welcome_02 extends CI_Controller class

    Welcome_03 extends CI_Controller class Welcome_04 extends CI_Controller class Welcome_05 extends CI_Controller class Welcome_06 extends CI_Controller class Welcome_07 extends CI_Controller class Welcome_08 extends CI_Controller Controller 都需要有共同的變數及函數
  31. 99 class Welcome_01 extends MY_Controller class Welcome_02 extends MY_Controller class

    Welcome_03 extends MY_Controller class Welcome_04 extends MY_Controller class Welcome_05 extends MY_Controller class Welcome_06 extends MY_Controller class Welcome_07 extends MY_Controller class Welcome_08 extends MY_Controller 更改變數及函數則大家一起變動
  32. 102 直接更換核心類別  放置目錄 application/core/  更換 input 核心 

    application/core/input.php class CI_Input { public function __construct() {} }
  33. 103 擴充核心類別  類別宣告必須繼承父類別  新類別名稱與檔名必須使用 MY_ 前置字串  命名

    : application/core/MY_Input.php class MY_Input extends CI_Input { function __construct() { parent::__construct(); } }
  34. 105 作業  練習擴充 CI_Controller 核心  index.php/welcome/?lang=english  index.php/welcome/?lang=zh­tw

     擴充核心取得 $_GET['lang'] 變數資料  利用此變數將 language 檔案載入實現多國語系
  35. 106 目標  會員註冊系統  Email 認証信  Form 表單驗證

     多國語系  會員資料編輯  個人照片上傳  會員登入