公共函數(shù)和全局常量?

CodeIgniter 你可以在任何地方使用它們,并且不需要加載任何 類庫(kù)或輔助函數(shù)。

公共函數(shù)?

服務(wù)訪問(wèn)器函數(shù)?

cache([$key])?
參數(shù):
  • $key (string) – 需從緩存中檢索的參數(shù)名 (可選)
返回:

緩存對(duì)象或從緩存取回的變量

返回類型:

mixed

若 $key 不存在, 則返回緩存引擎實(shí)例. 若 $key有值存在, 則返回 $key 當(dāng)前存儲(chǔ)在緩存中的值,若值不存在則返回null。

Examples:

$foo = cache('foo');
$cache = cache();
env($key[, $default=null])?
參數(shù):
  • $key (string) – 需檢索的環(huán)境變量中的參數(shù)名
  • $default (mixed) – 如參數(shù)值不存在則返回默認(rèn)值.
返回:

運(yùn)行環(huán)境變量, 默認(rèn)值, 或者 null.

返回類型:

mixed

用于檢索事前設(shè)置在環(huán)境變量中的變量值,若無(wú)設(shè)置則返回默認(rèn)值. 若沒(méi)有找到健值則返回一個(gè)布爾值結(jié)果(false).

在特定的運(yùn)行環(huán)境中利用 .env 文件設(shè)置環(huán)境變量非常有用,例如數(shù)據(jù)庫(kù)設(shè)置,API 健值等.

esc($data, $context='html'[, $encoding])?
參數(shù):
  • $data (string|array) – 被輸出的信息.
  • $context (string) – 被輸出內(nèi)容的上下文. 默認(rèn)值 ‘html’.
  • $encoding (string) – 編碼字符串.
返回:

輸出的數(shù)據(jù)(The escaped data).

返回類型:

mixed

頁(yè)面中包含的輸出數(shù)據(jù), 它在防止 XSS 攻擊時(shí)很有用。 使用Laminas Escaper 庫(kù)來(lái)處理實(shí)際的數(shù)據(jù)過(guò)濾。

若 $data 為字符串, 則簡(jiǎn)單轉(zhuǎn)義并且返回。 若 $data 為數(shù)組, 則遍歷數(shù)組,轉(zhuǎn)義 key/value 鍵值對(duì)中的 ‘value’。

有效的 context 值: html, js, css, url, attr, raw, null

helper($filename)?
參數(shù):
  • $filename (string|array) – 加載的輔助類文件的名稱,或一個(gè)包含類文件名的數(shù)組。

    加載輔助類文件。

詳情參照 輔助函數(shù) 頁(yè).
lang($line[, $args[, $locale]])?
參數(shù):
  • $line (string) – 檢索文本的行
  • $args (array) – 一組數(shù)組數(shù)據(jù),用于替代占位符.
  • $locale (string) – 使用不同的地區(qū),而不是默認(rèn)的地區(qū)設(shè)置。

檢索一個(gè)基于某個(gè)別名字符串的本地特定文件。

更多詳細(xì)信息請(qǐng)見(jiàn) Localization 頁(yè)。

model($name[, $getShared = true[, &$conn = null]])?
參數(shù):
  • $name (string) –
  • $getShared (boolean) –
  • $conn (ConnectionInterface|null) –
返回:

More simple way of getting model instances

返回類型:

mixed

old($key[, $default = null[, $escape = 'html']])?
參數(shù):
  • $key (string) – 需要使用的原有的表單提交的鍵。
  • $default (mixed) – 如果當(dāng)$key不存在時(shí)返回的默認(rèn)值。
  • $escape (mixed) – 一個(gè) escape 的上下文,或傳值false來(lái)禁用該功能。
返回:

給定的鍵對(duì)應(yīng)的值,或設(shè)置的默認(rèn)值

返回類型:

mixed

提供了一個(gè)簡(jiǎn)易的方式,在表單提交時(shí)訪問(wèn) “原有的輸入數(shù)據(jù)”。

示例:

// 在控制器中查看表單提交
if (! $model->save($user))
{
    // 'withInput'方法意味著"原有的數(shù)據(jù)"需要被存儲(chǔ)。
    return redirect()->back()->withInput();
}

// 視圖中
<input type="email" name="email" value="<?= old('email') ?>">
// 以數(shù)組的形式
<input type="email" name="user[email]" value="<?= old('user.email') ?>">

注解

如果你正使用 :doc: form helper </helpers/form_helper> , 這個(gè)特性就是內(nèi)置的。只有在你不使用form helper的時(shí)候才需要手動(dòng)調(diào)用。

session([$key])?
變量 string $key:在session中查找的健值名稱.
返回:$key的值或者null,若$key不存在則返回一個(gè)session object實(shí)例。
返回類型:mixed

提供一個(gè)訪問(wèn) session 類和檢索存儲(chǔ)值的便捷方法。更多信息詳見(jiàn) the Sessions 頁(yè).

timer([$name])?
參數(shù):
  • $name (string) – 檢測(cè)點(diǎn)的名稱.
返回:

Timer 實(shí)例

返回類型:

CodeIgniterDebugTimer

提供一個(gè)快速訪問(wèn) Timer class的便捷的方法。 你可以將基準(zhǔn)點(diǎn)的名稱作為唯一參數(shù)傳遞。這將從這一點(diǎn)開(kāi)始計(jì)時(shí), 如果這個(gè)名稱的計(jì)時(shí)器已經(jīng)運(yùn)行,則停止計(jì)時(shí)。

示例:

// 獲取一個(gè)timer實(shí)例
$timer = timer();

// 設(shè)置計(jì)時(shí)器的開(kāi)始與結(jié)束點(diǎn)
timer('controller_loading');    // 開(kāi)始計(jì)時(shí)器
. . .
timer('controller_loading');    // 停止計(jì)時(shí)器運(yùn)行
view($name[, $data[, $options]])?
參數(shù):
  • $name (string) – 被加載的文件名
  • $data (array) – 鍵值對(duì)數(shù)組,在視圖中能被獲取。
  • $options (array) – 可選的參數(shù)數(shù)組,用于傳遞值給渲染類.
返回:

視圖的輸出.

返回類型:

string

抓取當(dāng)前的 RendererInterface-compatible 類(界面渲染類),告訴它展示特定的視圖。給控制器、庫(kù)、路由閉包提供了一種便捷的方法。

目前,在 $options 數(shù)組里只有一個(gè)選項(xiàng)是可用的,saveData 指定在同一個(gè)請(qǐng)求中,在多次調(diào)用 view() 時(shí)數(shù)據(jù)將連續(xù)。默認(rèn)情況下, 在顯示該單一視圖文件之后,該視圖的數(shù)據(jù)被丟棄。

$option 數(shù)組主要用于與第三方庫(kù)整合,例如Twig。

示例:

$data = ['user' => $user];

echo view('user_profile', $data);

詳情參見(jiàn) the Views 頁(yè)。

view_cell($library[, $params = null[, $ttl = 0[, $cacheName = null]]])?
參數(shù):
  • $library (string) –
  • $params (null) –
  • $ttl (integer) –
  • $cacheName (string|null) –
返回:

View cells are used within views to insert HTML chunks that are managed by other classes.

返回類型:

string

For more details, see the View Cells page.

其他函數(shù)?

app_timezone()?
返回:The timezone the application has been set to display dates in.
返回類型:string

Returns the timezone the application has been set to display dates in.

csrf_token()?
返回:當(dāng)前 CSRF token 名稱。
返回類型:string

返回當(dāng)前 CSRF token名稱。

csrf_header()?
returns:The name of the header for current CSRF token.
rtype:string

The name of the header for current CSRF token.

csrf_hash()?
返回:當(dāng)前 CSRF hash值.
返回類型:string

返回當(dāng)前 CSRF hash 的值.

csrf_field()?
返回:帶有全部請(qǐng)求CSRF信息的隱藏input的HTML字符串。
返回類型:string

返回已插入CSRF信息的隱藏input:

<input type=”hidden” name=”{csrf_token}” value=”{csrf_hash}”>
csrf_meta()?
返回:A string with the HTML for meta tag with all required CSRF information.
返回類型:string

Returns a meta tag with the CSRF information already inserted:

<meta name=”{csrf_header}” content=”{csrf_hash}”>
force_https($duration = 31536000[, $request = null[, $response = null]])?
參數(shù):
  • $duration (int) – 瀏覽器的秒數(shù)應(yīng)該將此資源的鏈接轉(zhuǎn)換為 HTTPS 。
  • $request (RequestInterface) – 當(dāng)前請(qǐng)求對(duì)象的實(shí)例。
  • $response (ResponseInterface) – 當(dāng)前響應(yīng)對(duì)象的實(shí)例。

檢查頁(yè)面當(dāng)前是否通過(guò)HTTPS訪問(wèn),如果不是,則用戶通過(guò)HTTPS重定向回當(dāng)前URI。 將設(shè)置 HTTP 嚴(yán)格的傳輸安全標(biāo)頭,該命令指示現(xiàn)代瀏覽器自動(dòng)將HTTP請(qǐng)求修改為 $duration 參數(shù)時(shí)間的HTTPS請(qǐng)求。

function_usable($function_name)?
參數(shù):
  • $function_name (string) – Function to check for
返回:

TRUE if the function exists and is safe to call, FALSE otherwise.

返回類型:

bool

is_cli()?
返回:如果腳本是從命令行執(zhí)行的,則為true,否則為false。
返回類型:bool
is_really_writable($file)?
參數(shù):
  • $file (string) – The filename being checked.
返回:

TRUE if you can write to the file, FALSE otherwise.

返回類型:

bool

log_message($level, $message[, $context])?
參數(shù):
  • $level (string) – 級(jí)別程度
  • $message (string) – 寫(xiě)入日志的信息.
  • $context (array) – 一個(gè)標(biāo)記和值的聯(lián)合數(shù)組被替換到 $message
返回:

如果寫(xiě)入日志成功則為 TRUE ,如果寫(xiě)入日志出現(xiàn)問(wèn)題則為 FALSE 。

返回類型:

bool

使用 app/Config/Logger.php 中定義的日志處理程序記錄日志。

級(jí)別可為以下值: emergency, alert, critical, error, warning, notice, info, or debug.

Context 可用于替換 message 字符串中的值。詳情參見(jiàn) the:doc:Logging Information <logging> 頁(yè)。

redirect(string $uri)?
參數(shù):
  • $uri (string) – 需要引導(dǎo)用戶重定向到的頁(yè)面.

返回以后RedirectResponse的實(shí)例以便創(chuàng)建重定向:

// 回到上一個(gè)頁(yè)面
return redirect()->back();

// 跳轉(zhuǎn)至具體的 URL
return redirect()->to('/admin');

// 跳轉(zhuǎn)到一個(gè)命名路由或反向路由 URI
return redirect()->route('named_route');

// 在跳轉(zhuǎn)中保持原有的輸入值,使得它們可以被 `old()` 函數(shù)調(diào)用。
return redirect()->back()->withInput();

// 顯示一個(gè)消息
return redirect()->back()->with('foo', 'message');

當(dāng)將URI傳給這個(gè)函數(shù)時(shí)。它將會(huì)被作為一個(gè)反向路由請(qǐng)求,而不是一個(gè)完整的 URI ,就像使用 redirect()->route()一樣:

// 跳轉(zhuǎn)到一個(gè)命名路由或反向路由 URI
return redirect('named_route');
remove_invisible_characters($str[, $urlEncoded = TRUE])?
參數(shù):
  • $str (string) – 輸入字符串
  • $urlEncoded (bool) – 是否移除URL編碼字符
返回:

已過(guò)濾的字符串

返回類型:

string

這個(gè)函數(shù)防止在 ASCII 字符之間插入空字符(NULL),例如 Java\0script。

示例:

remove_invisible_characters('Java\\0script');
// 返回: 'Javascript'
route_to($method[, ...$params])?
參數(shù):
  • $method (string) – 命名路由別名, 或匹配controller/method名稱。
  • $params (mixed) – 一個(gè)或更多參數(shù)被傳遞到路由中匹配。

以指定的路由別名或 controller::method 組合為依據(jù)生成一個(gè)相對(duì) URI 。如果提供參數(shù),將執(zhí)行參數(shù)。

詳情參見(jiàn) the URI 路由 頁(yè)。

service($name[, ...$params])?
參數(shù):
  • $name (string) – 加載的服務(wù)名稱
  • $params (mixed) – 一個(gè)或多個(gè)參數(shù)傳遞到服務(wù)方法。
返回:

指定的服務(wù)類的實(shí)例。

返回類型:

mixed

提供簡(jiǎn)易訪問(wèn)任何在系統(tǒng)中定義的服務(wù),詳見(jiàn)the Services 。 這將總是返回類的共享實(shí)例,因此不管在單個(gè)請(qǐng)求中調(diào)用多少次,都只會(huì)創(chuàng)建一個(gè)類實(shí)例。

示例:

$logger = service('logger');
$renderer = service('renderer', APPPATH.'views/');
single_service($name[, ...$params])?
參數(shù):
  • $name (string) – 加載的服務(wù)名稱
  • $params (mixed) – 一個(gè)或多個(gè)參數(shù)傳遞到服務(wù)方法。
返回:

指定的服務(wù)類的實(shí)例。

返回類型:

mixed

等同于前面所描述的 service() 函數(shù), 除了所有調(diào)用該函數(shù)將返回一個(gè)類的新實(shí)例。
service 返回的是相同的實(shí)例。
slash_item($item)?
參數(shù):
  • $item (string) – Config item name
返回:

The configuration item or NULL if the item doesn’t exist

返回類型:

string|null

Fetch a config file item with slash appended (if not empty)

stringify_attributes($attributes[, $js])?
參數(shù):
  • $attributes (mixed) – 字符串, 鍵值對(duì)數(shù)組, 或者對(duì)象
  • $js (boolean) – TRUE 若值不需要引用 (Javascript風(fēng)格)
返回:

字符串包含鍵值對(duì)屬性, 逗號(hào)分隔

返回類型:

string

輔助函數(shù)用于轉(zhuǎn)換字符串, 數(shù)組, 或者字符串的對(duì)象屬性。

全局常量?

以下的常量在你的應(yīng)用中的任何地方有效。

核心常量?

constant APPPATH?

app 目錄的路徑。

constant ROOTPATH?

項(xiàng)目根目錄,APPPATH 目錄的上層目錄。

constant SYSTEMPATH?

system 目錄的路徑。

constant FCPATH?

保存的前端控制器目錄的路徑。

constant WRITEPATH?

writable 目錄的路徑。

時(shí)間常量?

constant SECOND?

等于 1.

constant MINUTE?

等于 60.

constant HOUR?

等于 3600.

constant DAY?

等于 86400.

constant WEEK?

等于 604800.

constant MONTH?

等于 2592000.

constant YEAR?

等于 31536000.

constant DECADE?

等于 315360000.