?
This document uses PHP Chinese website manual Release
Output類是具有一個主要功能的核心類:將最終化的網(wǎng)頁發(fā)送到請求瀏覽器。如果您使用該功能,它也負責(zé)緩存您的網(wǎng)頁。
注
此類由系統(tǒng)自動初始化,因此不需要手動進行。
在正常情況下,您甚至不會注意到Output類,因為它在透明的情況下運行而無需您的干預(yù)。例如,當您使用Loader類加載視圖文件時,它會自動傳遞給Output類,該類將在系統(tǒng)執(zhí)行結(jié)束時由CodeIgniter自動調(diào)用。但是,如果需要,可以手動干預(yù)輸出。
class CI_Output$parse_exec_vars = TRUE;
啟用/禁用解析{elapsed_time}和{memory_usage}偽變量。
默認情況下,CodeIgniter將在輸出中解析這些標記。若要禁用此屬性,請在控制器中將此屬性設(shè)置為false。
$this->output->parse_exec_vars = FALSE;
set_output($output)
參數(shù): | $ output(string) - 將輸出設(shè)置為的字符串 |
---|---|
返回: | CI_Output實例(方法鏈接) |
返回類型: | CI_Output |
$ output(string) - 將輸出設(shè)置為的字符串返回:CI_Output實例(方法鏈接)返回類型:CI_Output允許您手動設(shè)置最終輸出字符串。用法示例:$ this-> output-> set_output($ data); 重要如果您手動設(shè)置輸出,它必須是您調(diào)用它的函數(shù)中完成的最后一件事。例如,如果您在其中一個控制器方法中構(gòu)建頁面,則不要將輸出設(shè)置為結(jié)束。set_content_type($mime_type[, $charset = NULL])
參數(shù):$ mime_type(string) - MIME類型idenitifer字符串$ charset(字符串) - 字符集返回:CI_Output實例(方法鏈接)返回類型:CI_Output
$ mime_type(string) - MIME類型idenitifer字符串
$ charset(字符串) - 字符集
Returns: CI\_Output instance (method chaining)
Return type: CI\_Output
Permits you to set the mime-type of your page so you can serve JSON data, JPEG’s, XML, etc easily.
$this->output ->set_content_type('application/json') ->set_output(json_encode(array('foo' => 'bar'))); $this->output ->set_content_type('jpeg') // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php ->set_output(file_get_contents('files/something.jpg'));
重要
確保您傳遞給此方法的任何非MIME字符串存在于application / config / mimes.php中,否則它將不起作用。
還可以通過傳遞第二個參數(shù)來設(shè)置文檔的字符集:
$ this-> output-> set_content_type('css','utf-8');
get_content_type()
返回: | 內(nèi)容類型字符串 |
---|---|
返回類型: | 串 |
get_header($header)
參數(shù): | $ header(string) - HTTP標頭名稱 |
---|---|
返回: | HTTP響應(yīng)頭或NULL,如果未找到 |
返回類型: | mixed |
$ header(string) - HTTP頭名稱返回:HTTP響應(yīng)頭或NULL,如果未找到返回類型:mixed返回請求的HTTP頭值,或者如果未設(shè)置請求的頭,則返回NULL。例如:$ this-> output-> set_content_type('text / plain','UTF-8'); echo $ this-> output-> get_header('content-type'); //輸出:text / plain; charset = utf-8注意標題名稱以不區(qū)分大小寫的方式進行比較。注意header()
還會檢測通過PHP本地函數(shù)發(fā)送的原始頭文件。get_output()
返回:輸出string返回類型:字符串append_output($output)
參數(shù):$ output(string) - appendReturns的附加輸出數(shù)據(jù):CI_Output實例(方法鏈接)返回類型:CI_Output
$ output(string) - 附加的輸出數(shù)據(jù)
Returns: CI\_Output instance (method chaining)
Return type: CI\_Output
將數(shù)據(jù)追加到輸出字符串中。
$this - >輸出 - > append_output($數(shù)據(jù));
set_header($header[, $replace = TRUE])
參數(shù): | $ header(string) - HTTP響應(yīng)頭文件$ replace(bool) - 是否替換舊的頭文件值(如果已設(shè)置) |
---|---|
返回: | CI_Output實例(方法鏈接) |
返回類型: | CI_Output |
$ header(string) - HTTP響應(yīng)頭
$ replace(bool) - 是否替換舊的標題值,如果它已經(jīng)設(shè)置
Returns: CI\_Output instance (method chaining)
Return type: CI\_Output
Permits you to manually set server headers, which the output class will send for you when outputting the final rendered display. Example:
$ this-> output-> set_header('HTTP / 1.0 200 OK'); $ this-> output-> set_header('HTTP / 1.1 200 OK'); $ this-> output-> set_header('Last-Modified:'.gmdate('D,d MYH:i:s',$ last_update)。'GMT'); $ this-> output-> set_header('Cache-Control:no-store,no-cache,must-revalidate'); $ this-> output-> set_header('Cache-Control:post-check = 0,pre-check = 0'); $ this-> output-> set_header('Pragma:no-cache');
set_status_header([$code = 200[, $text = '']])
參數(shù): | $ code(int) - HTTP狀態(tài)碼$ text(字符串) - 可選消息 |
---|---|
返回: | CI_Output實例(方法鏈接) |
返回類型: | CI_Output |
$ code(int) - HTTP狀態(tài)碼
$ text(string) - 可選消息
Returns: CI\_Output instance (method chaining)
Return type: CI\_Output
允許您手動設(shè)置服務(wù)器狀態(tài)標題。例:
$this- >輸出 - > set_status_header(401); //將頭部設(shè)置為:未授權(quán)
注
此方法是公共函數(shù)set_status_header()
...
enable_profiler([$val = TRUE])
參數(shù): | $ val(bool) - 是否啟用或禁用Profiler |
---|---|
返回: | CI_Output實例(方法鏈接) |
返回類型: | CI_Output |
$ val(bool) - 是否啟用或禁用Profiler返回:CI_Output實例(方法鏈)返回類型:CI_Output允許您啟用/禁用Profiler,它將在頁面底部顯示基準測試和其他數(shù)據(jù)以進行調(diào)試和優(yōu)化目的。要啟用Profiler,請在Controller方法的任何位置放置以下行:$ this-> output-> enable_profiler(TRUE); 啟用后,報告將生成并插入到頁面的底部。要禁用分析器,您可以使用:$ this-> output-> enable_profiler(FALSE); set_profiler_sections($sections)
參數(shù):$ sections(array) - Profiler節(jié)返回:CI_Output實例(方法鏈)返回類型:CI_Output
$ sections(數(shù)組) - Profiler部分
Returns: CI\_Output instance (method chaining)
Return type: CI\_Output
允許您在啟用時啟用/禁用Profiler的特定部分。有關(guān)更多信息,請參閱[Profiler](../ general / profiling)文檔。
cache($time)
參數(shù): | $ time(int) - 以分鐘為單位的緩存過期時間 |
---|---|
返回: | CI_Output實例(方法鏈接) |
返回類型: | CI_Output |
$ time(int) - 以分鐘為單位的緩存過期時間返回:CI_Output實例(方法鏈接)返回類型:CI_Output將當前頁面緩存指定的分鐘數(shù)。有關(guān)更多信息,請參閱緩存文檔。_display([$output = ''])
參數(shù):$ output(string) - 輸出數(shù)據(jù)overrideReturns:void返回類型:void
$ output(string) - 輸出數(shù)據(jù)覆蓋
Returns: void
Return type: void
Sends finalized output data to the browser along with any server headers. It also stops benchmark timers.
注
此方法在腳本執(zhí)行結(jié)束時自動調(diào)用,除非您使用以下命令中止腳本執(zhí)行,否則不需要手動調(diào)用它exit()
或die()
在你的密碼里。
例子:
$response = array('status' => 'OK'); $this->output ->set_status_header(200) ->set_content_type('application/json', 'utf-8') ->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) ->_display(); exit;
注
手動調(diào)用此方法而不中止腳本執(zhí)行將導(dǎo)致重復(fù)輸出。