Codeigniter2學習筆記

0、控制器類必須繼承CI_Controller,類名首字母必須大寫
   (默認)example.com/index.php/控制器名/方法名/其它附加的   
    (控制器名必須與對應控制器文件名大小寫一樣,方法名也要大小寫一樣)
 
1、URL向方法傳遞參數,example.com/index.php/products/shoes/a/b
<?php
class Products extends CI_Controller {
    public function shoes($a, $b)
    {
        echo $a;
        echo $b;
    }
}
?>
結果是ab

2、修改默認的“URL調用方法”的行爲
在Controller中定義_remap方法,那麼it will always get called regardless of what your URI contains.
public function _remap($method)
{
    if ($method == 'some_method')
    {
        $this->$method();
    }
    else
    {
        $this->default_method();
    }
}
 _remap()有第二個可選的參數.
public function _remap($method, $params = array())
{
    $method = 'process_'.$method;
    if (method_exists($this, $method))
    {
        return call_user_func_array(array($this, $method), $params);
    }
    show_404();
}

3、控制頁面輸出,
   在控制器中定義_output方法,最後頁面輸出數據不會發送到瀏覽器會傳遞給_output處理
public function _output($output)
{
    echo $output;
}

4、在頁面最下面顯示調試信息
   在控制器方法中$this->output->enable_profiler(true);//禁用是false默認是禁用
   在application/config/profiler.php中可以配置是否顯示調試信息中的每塊內容
   $config['config']= FALSE;
   $config['queries']= FALSE;
   也可以在控制器中:
   $sections = array(
    'config'  => TRUE,
    'queries' => TRUE
    );
   $this->output->set_profiler_sections($sections);
   到達動態控制調試信息的顯示。
   $sections所有可用的key
          Key                         描述                                                默認值
       benchmarks            Elapsed time of Benchmark points and total execution time     TRUE
       config                系統配置變量                                                  TRUE
       controller_info       訪問的控制器名和方法名                                        TRUE
       get                   GET的數據                                                  TRUE
       http_headers          http請求頭信息                                                TRUE
       memory_usage          內存使用單位byte                                              TRUE
       post                  POST的數據                                                    TRUE
       queries               所有執行的SQL語句包括執行時間                                 TRUE
       uri_string            當前請求的URL                                                 TRUE
       query_toggle_count 超過多少條查詢隱藏查詢信息塊                                  25
   
    CodeIgniter will parse the pseudo-variables 0.3945 and 3.04MB in y
    our output by default. To disable this, set the $parse_exec_vars class property to FALSE in your controller.
    $this->output->parse_exec_vars = FALSE;

5、控制器中不想被URL訪問的方法
   方法用private修飾並且方法名以下劃線開始,那麼這個方法就不能通過URL訪問
    private function _utility()
   {
   // some code
   }

6、控制器controllers文件夾中建立子文件夾功能,
   訪問時變爲example.com/index.php/子文件夾名/控制器名/方法名/其它附加的    

7、子類控制器有構造方法時必須先調用父類構造方法parent::__construct();

8、系統保留名稱,避免在自己的代碼中出現。
  控制器名
       CI_Base
       _ci_initialize
       Default
       index

  方法名
      is_really_writable()
      load_class()
      get_config()
      config_item()
      show_error()
      show_404()
      log_message()
      _exception_handler()
      get_instance()

   變量
      $config
      $mimes
      $lang

   常量
      ENVIRONMENT
      EXT
      FCPATH
      SELF
      BASEPATH
      APPPATH
      CI_VERSION
      FILE_READ_MODE
      FILE_WRITE_MODE
      DIR_READ_MODE
      DIR_WRITE_MODE
      FOPEN_READ
      FOPEN_READ_WRITE
      FOPEN_WRITE_CREATE_DESTRUCTIVE
      FOPEN_READ_WRITE_CREATE_DESTRUCTIVE
      FOPEN_WRITE_CREATE
      FOPEN_READ_WRITE_CREATE
      FOPEN_WRITE_CREATE_STRICT
      FOPEN_READ_WRITE_CREATE_STRICT
9、視圖永遠不會被直接調用,必須被控制加載
  加載視圖文件name.php $this->load->view('name');如果是name.html那麼就寫爲 $this->load->view('name.html');
  可以同時加載多個視圖
  <?php
  class Page extends CI_Controller {
     function index()
     {
        $data['page_title'] = 'Your title';
        $this->load->view('header');
        $this->load->view('menu');
        $this->load->view('content', $data);
        $this->load->view('footer');
     }

  }
  ?>
  視圖文件夾可以建立子文件夾,那麼加載視圖時變爲:
  $this->load->view('folder_name/file_name');
10、向視圖傳遞數據:

   數據是數組:
   $data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
          );
   $this->load->view('blogview', $data);
  視圖中輸出:
  <?=$title?>

  數據也可以是對象:
  $data = new Someclass();
  $this->load->view('blogview', $data);
  視圖中輸出:
  <?=對象屬性名?>
11、循環數據
 <?php
class Blog extends CI_Controller {
    function index()
    {
        $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";
        $this->load->view('blogview', $data);
    }
}
?>
視圖中:
<ul>
 <?php foreach ($todo_list as $item):?>
    <li><?php echo $item;?></li>
<?php endforeach;?>
</ul>
上面用到了替換語法:類似的還有 endif, endfor, endforeach, and endwhile
快捷輸出<?=變量?>

官方提供也有一個模板引擎,但是爲了使頁面運行更快不建議使用,建議使用純php+語法替換

12、返回視圖數據,而不是輸出到瀏覽器
  $string = $this->load->view('myfile', '', true);
  第三參數This can be useful if you want to process the data in some way.
  If you set the parameter to true (boolean) it will return data.
  The default behavior is false, which sends it to your browser.
  Remember to assign it to a variable if you want the data returned:

13、URL安全,URL地址中只能包含這些字符: 數字 文本 ~ . : _ -
   魔法轉義magic_quotes_runtime在程序初始化時就被禁用了。
  you don't have to remove slashes when retrieving data from your database.

14、錯誤信息控制
  By default, CodeIgniter comes with the environment constant set to 'development'.
  At the top of index.php, you will see:
  define('ENVIRONMENT', 'development');
  Setting CodeIgniter's ENVIRONMENT constant in index.php to a value of 'production' will
  turn off these errors. In development mode, it is recommended that a value of 'development' is used.

15、使用system/libraries 或者application/libraries裏面的類庫時
   首先要加載$this->load->library('email');
   然後使用:$this->someclass->some_function();  // Object instances will always be lower case
   帶參初始化:
   $params = array('type' => 'large', 'color' => 'red');
   $this->load->library('Someclass', $params);
   $this->load之後創建一個類的實例$this->someclasss。

16、在自己的庫中利用CodeIgniter資源
The Database classes can not be extended or replaced with your own classes.
All other classes are able to be replaced/extended.
    $this, however, only works directly within your controllers, your models, or your views.
If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.

17、模型
The file name will be a lower case version of your class name. For example, if your class is this:
class User_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
}
Your file will be this:
application/models/user_model.php

18、加載模型
 $this->load->model('Model_name');
 $this->load->model('blog/Model_name');//blog是models一個子文件夾
 使用:$this->Model_name->function();
 別名:
  $this->load->model('Model_name', 'fubar');
  $this->fubar->function();等價於$this->Model_name->function();

19、加載系統庫system/libraries
    $this->load->library('email');
    $this->load->library(array('email', 'table'));

20、載入輔助函數
   例如,要載入文件名爲url_helper.php的URL Helper,你將會用到下面的語句:
   $this->load->helper('url');
   url 是輔助函數文件的名字(不帶.php後綴 和"helper" 部分)。
   載入多個輔助函數
   如果你想一次載入多個輔助函數,你可以這樣做:
   $this->load->helper( array('helper1', 'helper2', 'helper3') );
   一旦你載入了想要用到輔助函數文件,你就可以用標準的函數調用方法來使用裏面的函數。
   自動載入輔助函數
   如果你想要的話,CodeIgniter可以自動爲你載入輔助函數。你可以通過打開 application/config/autoload.php ,
   並往自動載入數組(autoload array)中增加輔助函數來實現。
   "擴展" Helpers
   你如果想 "擴展"一個原有的 Helpers,,可以在你的 application/helpers/ 目錄下創建一個新的helper,
   新的helper的名字是在被“擴展”的Helper的名字開頭多加一個 MY_ (這是可以配置的. 見下.).
   如果你想做的只是在原有的helper中添加一些新的功能,比如,添加一兩個新的方法,或者是修改一個方法;
   就不值得重寫自己的helper。在這種情況下,最好是“擴展”已有的helper。“擴展”一詞用在這裏不是很恰當,
   因爲Helper的方法是procedural 和 discrete的,在傳統的語言環境中無法被“擴展”,不過在CodeIgniter中,
   你可以添加或修改helper的方法。
   例如,擴展一個本地已有的 Array Helper 你應該建立一個文件: application/helpers/MY_array_helper.php,
   並添加或重寫(override)其中的一些方法:
   // any_in_array() is not in the Array Helper, so it defines a new function
   function any_in_array($needle, $haystack)
   {
       $needle = (is_array($needle)) ? $needle : array($needle);
       foreach ($needle as $item)
       {
           if (in_array($item, $haystack))
           {
               return TRUE;
           }
           }
       return FALSE;
   }
   // random_element() is included in Array Helper, so it overrides the native function
   function random_element($array)
   {
       shuffle($array);
       return array_pop($array);
   }
   設定你自己的前綴(Prefix)
   用於"擴展" helper 而加上前綴的文件同樣也是對庫和核心類的擴展.爲了設置你自定義的前綴,
   請打開 application/config/config.php 文件,然後找到如下的條目:
   $config['subclass_prefix'] = 'MY_';
   請注意所以CodeIgniter自帶的庫都被冠以 CI_ 這樣的前綴,所以請不使用這樣的自定義前綴.







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章