Loader 類別
讀取器 (Loader) 就如同名字一樣, 用來讀取元件. 這些元件可以是程式庫(libraries) (類別classes) 視圖(Views), 補助函數(Helpers), 模型(Models) 或是你自己的檔案。
注意: 此類別由系統自動初始化所以不需要手動載入。
此類別擁有以下的函式(functions):
$this->load->library('class_name', $config, 'object name')
這函式用來讀取核心類別. class_name 為你要讀取的類別名稱. 注意: 我們使用的術語 "類別(class)" 和 "程式庫(library)" 是可交換的.
舉例來說, 如果你要藉由CodeIgniter來發送E-mail, 第一步驟為在控制器(controller)讀取郵件類別(email class):
$this->load->library('email');
讀取後, 即可使用此郵件類別(mail class), 使用 $this->email->some_function().
程式庫(Library)檔案可以被存在主要的"程式庫libraries"子目錄中, 或是你自己的 application/libraries 目錄. 讀取位於子目錄中的程式庫(library)檔案, 簡單的加入相對路徑至程式庫(libraries)資料夾中. 舉例來說, 如果你的檔案位於:
libraries/flavors/chocolate.php
你必須使用以下程式來讀取它:
$this->load->library('flavors/chocolate');
你可以自行建立任何的子目錄來放置程式庫
此外,您可以同時間透過陣列傳遞來達到載入多個 library。
$this->load->library(array('email', 'table'));
設定選擇 Setting options
透過第二個參數來傳遞設定值(可選擇的)。典型的方式就透是過陣列 (array) 傳遞:
$config = array (
'mailtype' => 'html',
'charset' => 'utf-8,
'priority' => '1'
);
$this->load->library('email',$config);
設定選項(Setting options)可以經由單一的設定檔(config file)來設定. 每個程式庫(library)的詳細功能說明在各自的說明文件中, 所以請閱讀你想使用的程式庫的相關資訊.
請注意, 當您設定第一個參數,用陣列來載入多個 library,每個 Library 將會接受相同的參數設定資料。
指定程式庫(Library)為不同的物件名稱
如果第三個參數 (可選擇的) 為空值, 程式庫(library)通常被分配到一個與程式庫(library)擁有相同名稱的物件. 舉例來說, 如果程式庫(library)被命名為Session, 參數名將為 $this->session.
如果你希望設定為自己想要的名稱,你可以設置在第三個參數:
$this->load->library('session','','my_session');
// Session 類別的存取方式將為:
$this->my_session
請注意, 當您設定第一個參數,用陣列來載入多個 library,那麼第三個自訂參數將會失去作用。
$this->load->view('file_name', $data, true/false)
此函式用來載入您的 View 檔案。假如您尚未閱讀使用手冊中 Views 章節,那非常建議您先閱讀此章節,它會告訴您如何使用此函式。
第一個參數是必填的,請填入您要載入的 view 檔案。 注意: .php 副檔名並不需要被包含在第一個參數裏面,除非您使用的檔案名稱並非是 .php 結尾。
第二個選擇性參數可以是一個關聯式陣列或者是物件,並且透過 PHP extract 函式將物件或陣列轉換成變數使用在 View 檔案裡。再次透過閱讀 Views 章節了解更多使用方法。
第三個選擇性參數讓您改變函數的行為,它會將資料當作字串回傳,而不是直接輸出到瀏覽器。這作法非常有用,您可以透過設定參數為 true (boolean),函式將會回傳資料字串。函式預設行為是 false,將資料輸出到瀏覽器。假如您設定為 true,請記住將回傳資料分配給變數:
$string = $this->load->view('myfile', '', true);
$this->load->model('Model_name');
$this->load->model('Model_name');
假如您的 model 存放在 model 目錄底下的子目錄,那麼請把相對目錄也必須包含進來。舉例來說,假如您有一個 model 存放在 application/models/blog/queries.php,你應該使用底下的語句來載入它:
$this->load->model('blog/queries');
假如您想要自訂 model 名稱,請在函數的第二個參數中給出你自行定義的名稱:
$this->load->model('Model_name', 'fubar');
$this->fubar->function();
$this->load->database('options',true/false)
這個函數的作用是載入 database 類別,其中兩個參數都可以選擇性設定。更多詳細資訊請參考 database 章節。
$this->load->vars($array)
此函式接受一個關聯式陣列,使用 PHP extract 函數將陣列轉換成變數。此函數處理結果就如同上面 $this->load->view() 函數處理第二個參數是一樣的。假如您想要設定全域變數控制器內的建構子 (constructor of your controller),並且使這些變數可以任意用在控制器的 view 檔案,這些 view 檔案可以讓控制器內的任意函數所呼叫。你也可以重複呼叫 $this->load->vars($array) 函數,資料將會被快取且合併成單一陣列,並且透過 extract 轉換成多個變數。
$this->load->get_var($key)
此函數將會檢查目前您的 View 檔案關聯式陣列變數裡是否存在此變數。這不管對於您的變數是在 library 或者是其他 controller 裏面透過 $this->load->vars() 設定都非常有用。
$this->load->helper('file_name')
此函數載入 helper 檔案,file_name 檔名後面請不要加上 _helper.php。
$this->load->file('filepath/filename',true/false)
這是一般的載入函數。簡單的設定第一個參數路徑及檔案名稱,系統將會打開此檔案。就像是瀏覽檔案一樣,預設值會直接呈現在瀏覽器畫面, 如果設定第二個參數為true (boolean),它將會把檔案內容存放在變數裡面。
$this->load->language('file_name')
此函數同等於 語言(language) 載入函數: $this->lang->load()
$this->load->config('file_name')
此函數同等於 config 檔案載入函數: $this->config->load()
Application "Packages"
An application package allows for the easy distribution of complete sets of resources in a single directory, complete with its own libraries, models, helpers, config, and language files. It is recommended that these packages be placed in the application/third_party folder. Below is a sample map of an package directory
Sample Package "Foo Bar" Directory Map
The following is an example of a directory for an application package named "Foo Bar".
/application/third_party/foo_bar
config/
helpers/
language/
libraries/
models/
Whatever the purpose of the "Foo Bar" application package, it has its own config files, helpers, language files, libraries, and models. To use these resources in your controllers, you first need to tell the Loader that you are going to be loading resources from a package, by adding the package path.
$this->load->add_package_path()
Adding a package path instructs the Loader class to prepend a given path for subsequent requests for resources. As an example, the "Foo Bar" application package above has a library named Foo_bar.php. In our controller, we'd do the following:
$this->load->add_package_path(APPPATH.'third_party/foo_bar/');
$this->load->library('foo_bar');
$this->load->remove_package_path()
When your controller is finished using resources from an application package, and particularly if you have other application packages you want to work with, you may wish to remove the package path so the Loader no longer looks in that folder for resources. To remove the last path added, simply call the method with no parameters.
$this->load->remove_package_path()
Or to remove a specific package path, specify the same path previously given to add_package_path() for a package.:
$this->load->remove_package_path(APPPATH.'third_party/foo_bar/');
Package view files
By Default, package view files paths are set when add_package_path() is called. View paths are looped through, and once a match is encountered that view is loaded.
In this instance, it is possible for view naming collisions within packages to occur, and possibly the incorrect package being loaded. To ensure against this, set an optional second parameter of FALSE when calling add_package_path().
$this->load->add_package_path(APPPATH.'my_app', TRUE);
$this->load->view('my_app_index'); // Loads
$this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is TRUE
// Reset things
$this->load->remove_package_path(APPPATH.'my_app');
// Again without the second parameter:
$this->load->add_package_path(APPPATH.'my_app', TRUE);
$this->load->view('my_app_index'); // Loads
$this->load->view('welcome_message'); // Loads