CodeIgniter4 URL 輔助函數(shù)

2020-08-18 11:05 更新

URL 輔助函數(shù)文件包含的函數(shù)輔助 URLs 運行。

加載 URL 輔助函數(shù)

在每個請求中 URL 輔助函數(shù)由框架自動地加載。

通用函數(shù)

下文函數(shù)是通用的:

site_url([$uri = ''[, $protocol = NULL[, $altConfig = NULL]]])

參數(shù): $uri (string) – URI string
$protocol (string) – 協(xié)議,處理資料傳送的標準,例如 ‘http’ 或者 ‘https’
$altConfig (\Config\App) – 使用更替配置
返回: Site URL
返回類型: string

返回你的 site URL,就像在你的配置文件里說明的。 index.php 文件(或者在你的配置文件里任何你已經(jīng)設(shè)置在你網(wǎng)站的 index_page) 將會添加到 URL,如同你通過函數(shù)程序段的一些 URL,外加在你的配置文件中已經(jīng)設(shè)置的 url_suffix.

在你的 URL 改變的事件中,你被鼓勵在任何時間使用函數(shù)生成本地 URL 以便你的頁面將變得更加便攜。 程序段能隨意地像 string 或者 array 通過函數(shù)。下文是 string 事例:

echo site_url('news/local/123');

上文的事例返回的地址如下: http://example.com/index.php/news/local/123

這里是一個通過數(shù)組程序段的事例:

$segments = array('news', 'local', '123');
echo site_url($segments);

對不同的網(wǎng)站如果生成 URLs 你或許會找到比你的配置更有用的更替配置,該函數(shù)包含不同配置優(yōu)先權(quán)。 我們?yōu)閱卧獪y試框架本身使用這個函數(shù)。

base_url([$uri = ''[, $protocol = NULL]])

參數(shù): $uri (string) – URI string
$protocol (string) – 協(xié)議,處理資料傳送的標準,例如 ‘http’ 或者 ‘https’
返回: 基地址 URL
返回類型: string

返回你網(wǎng)站的基地址 URL, 如同在你配置文件里具體說明的。事例:

echo base_url();

如同 site_url()該函數(shù)返回相同的事件, 排除 index_page 或者 url_suffix 被附加的情況。

也如函數(shù) site_url(), 你能提供程序段如 string 或者 array. 這里是 string 事例:

echo base_url("blog/post/123");

上文事例返回的地址如下: http://example.com/blog/post/123

因為不同的 site_url() 函數(shù)是有用的, 你能提供 string 值到文件里,譬如圖片或者層疊式樣式表。 例如:

echo base_url("images/icons/edit.png");

上文的輸出函數(shù)將給你如下面的鏈接: http://example.com/images/icons/edit.png

current_url([$returnObject = false])

參數(shù): $returnObject (boolean) – True 如果你想要 URI 事例返回,代替 string.
返回: 最近的 URL
返回類型: string/URI

返回最近被瀏覽過的頁面的正確的 URL (包括程序段).

注解

引用下面的函數(shù)是同樣的:: base_url(uri_string());

previous_url([$returnObject = false])

參數(shù): $returnObject (boolean) – True 如果你想要 URI 事例返回,代替 string.
返回: URL 用戶以前通過的
返回類型: string/URI

返回完整頁面的 URL (包含程序段)是用戶以前通過的。

由于安全問題造成盲目的信任 HTTP_REFERER 系統(tǒng)變量,在對話里如果它是有用的 CodeIgniter 將儲存以前瀏覽的頁面。 這保證我們將常常使用已知且可信的源,如果對話已經(jīng)被加載了,或者是別的方式不能得到的,那么 HTTP_REFERER 的凈化版本將會被應(yīng)用。

uri_string()

返回: An URI string
返回類型: string

返回你的最近 URL 的路徑部分。例如,如果你的 URL是這樣的:

http://some-site.com/blog/comments/123

函數(shù)將返回:

blog/comments/123

index_page([$altConfig = NULL])

參數(shù): $altConfig (ConfigApp) – 使用更替配置
返回: ‘index_page’ 值
返回類型: mixed

返回你網(wǎng)站的 index_page, 如同在你的配置文件里明確說明的。 事例:

echo index_page();

如同用 site_url(),你也許要具體制定一個更替配置。 對不同的網(wǎng)站如果生成 URLs 你或許會找到比你現(xiàn)有的更有用的更替配置,函數(shù)包含不同配置優(yōu)先權(quán)。 我們?yōu)閱卧獪y試框架本身使用這個函數(shù)。

anchor([$uri = ''[, $title = ''[, $attributes = ''[, $altConfig = NULL]]]])

參數(shù): $uri (mixed) – URI 程序段的 URI string 或者 array
$title (string) – 錨定 title
$attributes (mixed) – HTML 屬性
$altConfig (ConfigApp) – 使用更替配置
返回: HTML 超連結(jié) (錨定 tag)
返回類型: string

基于你本地網(wǎng)站 URL 創(chuàng)建標準 HTML 錨定鏈接。

第一個參數(shù)能包含任意你希望應(yīng)用到 URL 的程序段。 如同上文用 site_url()函數(shù),程序段可以是 string 或者 array.

注解

如果你正在構(gòu)造的鏈接對于你的應(yīng)用是內(nèi)部的則不包含基地址 URL (http://…).

在你的配置文件里函數(shù)將會明確說明的從信息里被自動添加。你希望附加到的 URL 僅僅包含 URI 的程序段。

第二參數(shù)是你想要鏈接表達的正文。如果你留下第二個程序為空,URL 將會被應(yīng)用。

第三個參數(shù)包含你想要添加到鏈接里的的屬性列表。屬性可以是簡單的 string 或者組合數(shù)組。

這里是一些事例

echo anchor('news/local/123', 'My News', 'title="News title"');
// Prints: <a  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  title="News title">My News</a>


echo anchor('news/local/123', 'My News', array('title' => 'The best news!'));
// Prints: <a  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  title="The best news!">My News</a>


echo anchor('', 'Click here');
// Prints: <a  rel="external nofollow" target="_blank" >Click here</a>

如同上文闡述的,你也許可以明確說明更替配置。 如果對不同網(wǎng)站生成鏈接你也許會發(fā)現(xiàn)更替配置比你的配置是更有用的,它包含不同的配置優(yōu)先權(quán)。 我們?yōu)閱卧獪y試框架自身使用這個函數(shù)。

注解

屬性載入錨定函數(shù)是自動地退出對 XSS 攻擊不利的保護。

anchor_popup([$uri = ''[, $title = ''[, $attributes = FALSE[, $altConfig = NULL]]]])

參數(shù): $uri (string) – URI string
$title (string) – 錨定 title
$attributes (mixed) – HTML 屬性
$altConfig (ConfigApp) – 使用更替配置
返回: 自動跳起的 hyperlink
返回類型: string

幾乎同源于 anchor() 函數(shù),除了在新窗口里它是開放的 URL。 在第三個參數(shù)中你能明確說明 JavaScript 窗口屬性去控制窗口如何被打開。 如果第三個參數(shù)沒有設(shè)定,它將會帶著你自身的瀏覽器設(shè)定去簡單地打開一個新窗口。

這里是帶著屬性的事例:

$atts = array(
        'width'       => 800,
        'height'      => 600,
        'scrollbars'  => 'yes',
        'status'      => 'yes',
        'resizable'   => 'yes',
        'screenx'     => 0,
        'screeny'     => 0,
        'window_name' => '_blank'
);


echo anchor_popup('news/local/123', 'Click Me!', $atts);

注解

上文屬性是默認函數(shù)因此你僅僅需要去設(shè)置哪些個不同于你需要的屬性。

在第三個參數(shù)里如果你想要函數(shù)去簡單地通過空數(shù)組使用所有它的默認值:

echo anchor_popup('news/local/123', 'Click Me!', array());

注解

window_name 不是真實的屬性,但是對于 JavaScript 爭論 window.open() 方法, 它接受任何一方的窗口名或者窗口目標。

任何超過上文列表的其他屬性將會被分列就像 HTML 屬性對于錨定 tag. 如同上文描述的,你也許可以明確說明更替配置。 你也許會發(fā)現(xiàn)如果正生成的鏈接對不同的網(wǎng)站更替配置比你的配置更有用,他包含不同的配置優(yōu)先權(quán)。 我們?yōu)閱卧獪y試框架自身使用這個函數(shù)。

屬性載入錨定自動跳起函數(shù)是自動地退出對 XSS 攻擊不利的保護。

mailto($email[, $title = ''[, $attributes = '']])

參數(shù): $email (string) – E-mail 地址
$title (string) – 錨定 title
$attributes (mixed) – HTML 屬性
返回: “mail to” 超連結(jié)
返回類型: string

創(chuàng)建標準的 HTML 郵件鏈接。用法事例:

       echo mailto('me@my-site.com', 'Click Here to Contact Me');


如同用上文 :php:func:`anchor()` tab 函數(shù),
你可以使用第三個參數(shù)設(shè)定屬性::


       $attributes = array('title' => 'Mail me');
       echo mailto('me@my-site.com', 'Contact Me', $attributes);

注解

屬性載入錨定 mailto 函數(shù)是自動地退出對 XSS 攻擊不利的保護。

safe_mailto($email[, $title = ''[, $attributes = '']])

參數(shù): $email (string) – E-mail 地址
$title (string) – 錨定 title
$attributes (mixed) – HTML 屬性
返回: 安全垃圾郵件 “mail to” 超連結(jié)
返回類型: string

完全相似于 mailto() 函數(shù)除了 mailto tag 的模糊版本, 由于垃圾郵件群聊程序用 JavaScript 寫了該函數(shù)正使用序數(shù)數(shù)字用以從保護已經(jīng)收獲的 e-mail 地址。

auto_link($str[, $type = 'both'[, $popup = FALSE]])

參數(shù): $str (string) – 輸入 string
$type (string) – 鏈接類型 (‘email’, ‘url’ 或者 ‘both’)
$popup (bool) – 是否創(chuàng)建自動跳起鏈接
返回: 鏈接化的 string
返回類型: string

在字符到鏈接里自動地轉(zhuǎn)換包含 URLs 和 e-mail 地址。事例:

$string = auto_link($string);

第二參數(shù)決定是否 URLs 和 e-mail 是轉(zhuǎn)換了僅僅一個或者其他什么的。如果參數(shù)不是明確的說明默認行為是兼有的。 E-mail 鏈接編碼如同上文顯示的 safe_mailto() 一樣。

僅轉(zhuǎn)換 URLs:

$string = auto_link($string, 'url');

僅轉(zhuǎn)換 e-mail 地址:

$string = auto_link($string, 'email');

第三個參數(shù)決定是否鏈接在新窗口被顯示。 值是 TRUE 或者 FALSE (boolean):

$string = auto_link($string, 'both', TRUE);

注解

僅有的被普遍承認的 URLs 這些鏈接用 “www.” 或者用 “://” 開始。

url_title($str[, $separator = '-'[, $lowercase = FALSE]])

參數(shù): $str (string) – 輸入 string
$separator (string) – 字符分隔符
$lowercase (bool) – 是否轉(zhuǎn)換輸出 string 為小寫字型
返回: 已經(jīng)格式化的 string
返回類型: string

取 string 作為輸入值并創(chuàng)建友好人性化的 URL string. 這是有用的,例如,在 URL 里你有個blog ,在 blog 里你想要使用你的整個主題。事例:

$title     = "What's wrong with CSS?";
$url_title = url_title($title);
// Produces: Whats-wrong-with-CSS

第二個參數(shù)決定詞匯的定義符號。默認的破折號被使用。更好的選項是: - (破折號) 或者 _ (下劃線)。

事例:

$title     = "What's wrong with CSS?";
$url_title = url_title($title, 'underscore');
// Produces: Whats_wrong_with_CSS

第三個參數(shù)決定是或者不是小寫字符是被強迫的。默認他們不是。選項是 boolean TRUE/FALSE.

事例:

$title     = "What's wrong with CSS?";
$url_title = url_title($title, 'underscore', TRUE);
// Produces: whats_wrong_with_css

prep_url($str = '')

參數(shù): $str (string) – URL string
返回: 協(xié)議前綴 URL string
返回類型: string

在事件里這個函數(shù)正從一個 URL 錯過,它將添加 http:// 協(xié)議前綴。 通過 URL string 的函數(shù)像下文這樣:

$url = prep_url('example.com');
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號