裝載類
裝載,顧名思義,是用來裝載元素。這些元素可以是庫 (類) 視圖文件, 輔助函數(shù), 模型, 或者是你自己的文件。
提示: 這個類是由系統(tǒng)初始化的,所以,沒有必要自己手動初始化。
以下為這個類里面的函數(shù):
$this->load->library('class_name', $config, 'object name')
這個函數(shù)是用來加載核心類。class_name 是你要加載的類的名稱。 提示: “類”和“庫”是可替換使用的。
比如,你想用 CodeIgniter 來發(fā)送郵件,第一步就是在你的控制器里加載 email 類。
$this->load->library('email');
一旦被加載,就可以使用該類了,?使用 $this->email->some_function()。
類庫文件可以被保存到主libraries目錄的子目錄下面,或者保存到個人的application/libraries 目錄下。 要載入子目錄下的文件,只需將路徑包含進來就可以了,注意這里說的路徑是指相對于libraries目錄的路徑。 例如,當你有一個文件保存在下面這個位置:
libraries/flavors/chocolate.php
你應該使用下面的方式來載入它:
$this->load->library('flavors/chocolate');
你可以隨心所欲地將文件保存到多層的子目錄下。
Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load function.
$this->load->library(array('email', 'table'));
設置選項
第二個參數(shù)是可選的,作用是允許你有選擇性地傳遞配置參數(shù)。一般來說你可以將參數(shù)以數(shù)組的形式傳遞過去:
$config = array (
??????????????????'mailtype' => 'html',
??????????????????'charset'? => 'utf-8,
??????????????????'priority' => '1'
???????????????);
$this->load->library('email', $config);
配置參數(shù)通常也可以保存在一個配置文件中。每個類庫在其各自的頁面中都有詳細的說明,所以在使用類庫之前,你必須認真閱讀它們的說明。
Please take note, when multiple libraries are supplied in an array for the first parameter, each will receive the same parameter information.
給類庫分配不同的對象名
第三個參數(shù)也是可選的,如果為空,類庫通常就會被賦值給一個與類庫同名的對象。例如,如果類庫名為 Session, 它將會被賦值給一個名為 $this->session 的變量。
如果你希望使用你的自定義名稱,你可以通過第三個參數(shù)把它傳遞過去:
$this->load->library('session', '', 'my_session');
// Session 類現(xiàn)在可以通過下面的方式訪問:
$this->my_session
Please take note, when multiple libraries are supplied in an array for the first parameter, this parameter is discarded.
$this->load->view('file_name', $data, true/false)
這個函數(shù)是用來加載你的視圖文件。 如果你尚未閱讀本手冊視圖 章節(jié)的話,建議你先去閱讀那里的內(nèi)容,會有更詳細的函數(shù)使用說明.
第一個參數(shù)是必須的. 指定你要載入的視圖文件的名稱. ?注意: 無需加上 .php 擴展名,除非你使用了其他的擴展名。
第二個參數(shù) optional 允許你傳入數(shù)組或對象, 傳入的數(shù)組或對象將使用 php extract 函數(shù)導出,你可以在視圖中任意使用這些導出的變量. 此外,請閱讀 視圖 章節(jié)了解該功能的更多用法.
第三個參數(shù)是可選的,作用是改變函數(shù)的運行方式,將數(shù)據(jù)以字符串的形式返回,而不是發(fā)送給瀏覽器。當你希望用不同的方式處理數(shù)據(jù)時,這個參數(shù)就非常有用。如果你將這個參數(shù)設置為 true (布爾型),函數(shù)就會返回數(shù)據(jù)。這個參數(shù)的默認值是 false, 也就是數(shù)據(jù)將會被發(fā)送給瀏覽器。如果你希望數(shù)據(jù)被返回,記得要將它賦值給一個變量:
$string = $this->load->view('myfile', '', true);
$this->load->model('Model_name');
$this->load->model('Model_name');
如果你的模型位于models目錄的子目錄下,那么,相對路徑也必須被包含進來。例如,如果你有一個模型位于 application/models/blog/queries.php ,你應該使用下面的語句來載入它:
$this->load->model('blog/queries');
如果你希望給模型賦予一個你自定義的對象名,請在函數(shù)的第二個參數(shù)中給出你自定義的名稱:
$this->load->model('Model_name', 'fubar');
$this->fubar->function();
$this->load->database('options', true/false)
這個函數(shù)的作用是載入數(shù)據(jù)庫類。這兩個參數(shù)都是可選的。請查看 數(shù)據(jù)庫 段落以獲取更多信息。
$this->load->vars($array)
這個函數(shù)以一個關聯(lián)數(shù)組作為輸入?yún)?shù),將這個數(shù)組用PHP的extract函數(shù), 轉化成與這個數(shù)組對應的變量.這個函數(shù)如果用第二個參數(shù),將產(chǎn)生和上面的$this->load->view()相同的結果 .你之所以要單獨用這個函數(shù)也許是因為,你想在控制器的構造函數(shù)中設置一些全局變量,以使這些變量在任意函數(shù)調用的視圖(view)里能夠用上.你能多次調用這個函數(shù).數(shù)組數(shù)據(jù)被緩存并被并入一個數(shù)組,用來轉化成變量.
$this->load->get_var($key)
This function checks the associative array of variables available to your views. This is useful if for any reason a var is set in a library or another controller method using $this->load->vars().
$this->load->helper('file_name')
這個函數(shù)的作用是載入輔助函數(shù), file_name 是輔助函數(shù)對應的文件名,不包括 _helper.php 擴展名。
$this->load->file('filepath/filename', true/false)
這是一個通用的文件載入函數(shù)。在第一個參數(shù)中給出文件所在的路徑和文件名,對應的文件將會被打開。默認情況下,數(shù)據(jù)會被發(fā)送給瀏覽器,就如同視圖文件一樣,但如果你將第二個參數(shù)設置為 true (布爾型) 那么數(shù)據(jù)就會以字符串的形式被返回,而不是發(fā)送給瀏覽器。
$this->load->language('file_name')
這個函數(shù)是 語言加載函數(shù): $this->lang->load() 的一個別名。
$this->load->config('file_name')
這個函數(shù)是 配置文件加載函數(shù): $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
?