# URL 輔助函數
URL 輔助函數文件包含了一些幫助你處理 URL 的函數。
[TOC=2,3]
## [加載輔助函數](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#id3)
該輔助函數通過下面的代碼加載:
~~~
$this->load->helper('url');
~~~
## [可用函數](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#id4)
該輔助函數有下列可用函數:
site_url([$uri = ''[,?$protocol = NULL]])
參數:
* **$uri**?(string) -- URI string
* **$protocol**?(string) -- Protocol, e.g. 'http' or 'https'
返回: Site URL
返回類型: string
根據配置文件返回你的站點 URL 。index.php (獲取其他你在配置文件中設置的?**index_page**?參數) 將會包含在你的 URL 中,另外再加上你傳給函數的 URI 參數,以及配置文件中設置的?**url_suffix**?參數。
推薦在任何時候都使用這種方法來生成你的 URL ,這樣在你的 URL 變動時你的代碼將具有可移植性。
傳給函數的 URI 段參數可以是一個字符串,也可以是個數組,下面是字符串的例子:
~~~
echo site_url('news/local/123');
~~~
上例將返回類似于:http://example.com/index.php/news/local/123
下面是使用數組的例子:
~~~
$segments = array('news', 'local', '123');
echo site_url($segments);
~~~
該函數是?CI_Config::site_url()?的別名,更多信息請查閱?[配置類](http://codeigniter.org.cn/user_guide/libraries/config.html)?文檔。
base_url($uri = '',?$protocol = NULL)
參數:
* **$uri**?(string) -- URI string
* **$protocol**?(string) -- Protocol, e.g. 'http' or 'https'
返回: Base URL
返回類型: string
根據配置文件返回你站點的根 URL ,例如:
~~~
echo base_url();
~~~
該函數和?[site_url()](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#site_url "site_url")?函數相同,只是不會在 URL 的后面加上?index_page?或?url_suffix?。
另外,和?[site_url()](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#site_url "site_url")?一樣的是,你也可以使用字符串或數組格式的 URI 段。下面是字符串的例子:
~~~
echo base_url("blog/post/123");
~~~
上例將返回類似于:http://example.com/blog/post/123
跟?[site_url()](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#site_url "site_url")?函數不一樣的是,你可以指定一個文件路徑(例如圖片或樣式文件),這將很有用,例如:
~~~
echo base_url("images/icons/edit.png");
~~~
將返回類似于:http://example.com/images/icons/edit.png
該函數是?CI_Config::base_url()?的別名,更多信息請查閱?[配置類](http://codeigniter.org.cn/user_guide/libraries/config.html)?文檔。
current_url()
返回: The current URL
返回類型: string
返回當前正在瀏覽的頁面的完整 URL (包括分段)。
注解
該函數和調用下面的代碼效果是一樣的: site_url(uri_string());
uri_string()
返回: An URI string
返回類型: string
返回包含該函數的頁面的 URI 分段。例如,如果你的 URL 是:
~~~
http://some-site.com/blog/comments/123
~~~
函數將返回:
~~~
blog/comments/123
~~~
該函數是?CI_Config::uri_string()?的別名,更多信息請查閱?[配置類](http://codeigniter.org.cn/user_guide/libraries/config.html)?文檔。
index_page()
返回: 'index_page' value
返回類型: mixed
返回你在配置文件中配置的?**index_page**?參數,例如:
~~~
echo index_page();
~~~
anchor($uri = '',?$title = '',?$attributes = '')
參數:
* **$uri**?(string) -- URI string
* **$title**?(string) -- Anchor title
* **$attributes**?(mixed) -- HTML attributes
返回: HTML hyperlink (anchor tag)
返回類型: string
根據你提供的 URL 生成一個標準的 HTML 鏈接。
第一個參數可以包含任何你想添加到 URL 上的段,和上面的?[site_url()](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#site_url "site_url")?函數一樣,URL 的段可以是字符串或數組。
注解
如果你創建的鏈接是指向你自己的應用程序,那么不用包含根 URL (http://...)。 這個會根據你的配置文件自動添加到 URL 前面。所以你只需指定要添加的 URL 段就可以了。
第二個參數是鏈接的文本,如果留空,將使用鏈接本身作為文本。
第三個參數為你希望添加到鏈接的屬性,可以是一個字符串,也可以是個關聯數組。
這里是一些例子:
~~~
echo anchor('news/local/123', 'My News', 'title="News title"');
// Prints: <a href="http://example.com/index.php/news/local/123" title="News title">My News</a>
echo anchor('news/local/123', 'My News', array('title' => 'The best news!'));
// Prints: <a href="http://example.com/index.php/news/local/123" title="The best news!">My News</a>
echo anchor('', 'Click here');
// Prints: <a href="http://example.com">Click Here</a>
~~~
anchor_popup($uri = '',?$title = '',?$attributes = FALSE)
參數:
* **$uri**?(string) -- URI string
* **$title**?(string) -- Anchor title
* **$attributes**?(mixed) -- HTML attributes
返回: Pop-up hyperlink
返回類型: string
和?[anchor()](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#anchor "anchor")?函數非常類似,只是它生成的 URL 將會在新窗口被打開。你可以通過第三個參數指定 JavaScript 的窗口屬性,以此來控制窗口將如何被打開。如果沒有設置第三個參數,將會使用你的瀏覽器設置打開 一個新窗口。
這里是屬性的例子:
~~~
$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);
~~~
注解
上面的屬性是函數的默認值,所以你只需要設置和你想要的不一樣的參數。如果想使用所有默認的參數, 只要簡單的傳一個空數組即可: echo anchor_popup('news/local/123', 'Click Me!', array());
注解
**window_name**?其實并不算一個屬性,而是 Javascript 的?window.open() ?函數的一個參數而已, 該函數接受一個窗口名稱或一個 window 對象。
注解
任何不同于上面列出來的其他的屬性將會作為 HTML 鏈接的屬性。
mailto($email,?$title = '',?$attributes = '')
參數:
* **$email**?(string) -- E-mail address
* **$title**?(string) -- Anchor title
* **$attributes**?(mixed) -- HTML attributes
返回: A "mail to" hyperlink
返回類型: string
創建一個標準的 HTML e-mail 鏈接。例如:
~~~
echo mailto('me@my-site.com', 'Click Here to Contact Me');
~~~
和上面的?[anchor()](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#anchor "anchor")?函數一樣,你可以通過第三個參數設置屬性:
~~~
$attributes = array('title' => 'Mail me');
echo mailto('me@my-site.com', 'Contact Me', $attributes);
~~~
safe_mailto($email,?$title = '',?$attributes = '')
參數:
* **$email**?(string) -- E-mail address
* **$title**?(string) -- Anchor title
* **$attributes**?(mixed) -- HTML attributes
返回: A spam-safe "mail to" hyperlink
返回類型: string
和?[mailto()](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#mailto "mailto")?函數一樣,但是它的?mailto?標簽使用了一個混淆的寫法, 可以防止你的 e-mail 地址被垃圾郵件機器人爬到。
auto_link($str,?$type = 'both',?$popup = FALSE)
參數:
* **$str**?(string) -- Input string
* **$type**?(string) -- Link type ('email', 'url' or 'both')
* **$popup**?(bool) -- Whether to create popup links
返回: Linkified string
返回類型: string
將一個字符串中的 URL 和 e-mail 地址自動轉換為鏈接,例如:
~~~
$string = auto_link($string);
~~~
第二個參數用于決定是轉換 URL 還是 e-mail 地址,默認情況不指定該參數,兩者都會被轉換。 E-mail 地址的鏈接是使用上面介紹的[safe_mailto()](http://codeigniter.org.cn/user_guide/helpers/url_helper.html#safe_mailto "safe_mailto")?函數生成的。
只轉換 URL
~~~
$string = auto_link($string, 'url');
~~~
只轉換 e-mail 地址:
~~~
$string = auto_link($string, 'email');
~~~
第三個參數用于指定鏈接是否要在新窗口打開。可以是布爾值 TRUE 或 FALSE
~~~
$string = auto_link($string, 'both', TRUE);
~~~
url_title($str,?$separator = '-',?$lowercase = FALSE)
參數:
* **$str**?(string) -- Input string
* **$separator**?(string) -- Word separator
* **$lowercase**?(string) -- Whether to transform the output string to lower-case
返回: URL-formatted string
返回類型: string
將字符串轉換為對人類友好的 URL 字符串格式。例如,如果你有一個博客,你希望使用博客的標題作為 URL , 這時該函數很有用。例如:
~~~
$title = "What's wrong with CSS?";
$url_title = url_title($title);
// Produces: Whats-wrong-with-CSS
~~~
第二個參數指定分隔符,默認使用連字符。一般的選擇有:**-**?(連字符) 或者?**_**?(下劃線)
例如:
~~~
$title = "What's wrong with CSS?";
$url_title = url_title($title, 'underscore');
// Produces: Whats_wrong_with_CSS
~~~
注解
第二個參數連字符和下劃線的老的用法已經廢棄。
第三個參數指定是否強制轉換為小寫。默認不會,參數類型為布爾值 TRUE 或 FALSE 。
例如:
~~~
$title = "What's wrong with CSS?";
$url_title = url_title($title, 'underscore', TRUE);
// Produces: whats_wrong_with_css
~~~
prep_url($str = '')
參數:
* **$str**?(string) -- URL string
返回: Protocol-prefixed URL string
返回類型: string
當 URL 中缺少協議前綴部分時,使用該函數將會向 URL 中添加 http:// 。
像下面這樣使用該函數:
~~~
$url = prep_url('example.com');
~~~
redirect($uri = '',?$method = 'auto',?$code = NULL)
參數:
* **$uri**?(string) -- URI string
* **$method**?(string) -- Redirect method ('auto', 'location' or 'refresh')
* **$code**?(string) -- HTTP Response code (usually 302 or 303)
返回類型: void
通過 HTTP 頭重定向到指定 URL 。你可以指定一個完整的 URL ,也可以指定一個 URL 段, 該函數會根據配置文件自動生成改 URL 。
第二個參數用于指定一種重定向方法。可用的方法有:**auto**?、?**location**?和?**refresh**?。 location 方法速度快,但是在 ISS 服務器上不可靠。默認值為**auto**?,它會根據你的服務器環境 智能的選擇使用哪種方法。
第三個參數可選,允許你發送一個指定的 HTTP 狀態碼,這個可以用來為搜索引擎創建 301 重定向。 默認的狀態碼為 302 ,該參數只適用于**location**?重定向方法,對于?refresh?方法無效。例如:
~~~
if ($logged_in == FALSE)
{
redirect('/login/form/');
}
// with 301 redirect
redirect('/article/13', 'location', 301);
~~~
注解
為了讓該函數有效,它必須在任何內容輸出到瀏覽器之前被調用。因為輸出內容會使用服務器 HTTP 頭。
注解
為了更好的控制服務器頭,你應該使用?輸出類 ?的?set_header()?方法。
注解
使用 IIS 的用戶要注意,如果你隱藏了?Server?這個 HTTP 頭,?auto?方法將無法檢測到 IIS 。 在這種情況下,推薦你使用?**refresh**?方法。
注解
當使用 HTTP/1.1 的 POST 來訪問你的頁面時,如果你使用的是?**location**?方法,會自動使用 HTTP 303 狀態碼。
重要
該函數會終止腳本的執行。
- 歡迎使用 CodeIgniter
- 安裝說明
- 下載 CodeIgniter
- 安裝說明
- 從老版本升級
- 疑難解答
- CodeIgniter 概覽
- CodeIgniter 將從這里開始
- CodeIgniter 是什么?
- 支持特性
- 應用程序流程圖
- 模型-視圖-控制器
- 設計與架構目標
- 教程 - 內容提要
- 加載靜態內容
- 讀取新聞條目
- 創建新聞條目
- 結束語
- 常規主題
- CodeIgniter URL
- 控制器
- 保留名稱
- 視圖
- 模型
- 輔助函數
- 使用 CodeIgniter 類庫
- 創建類庫
- 使用 CodeIgniter 驅動器
- 創建驅動器
- 創建核心系統類
- 創建附屬類
- 鉤子 - 擴展框架核心
- 自動加載資源
- 公共函數
- 兼容性函數
- URI 路由
- 錯誤處理
- 網頁緩存
- 程序分析
- 以 CLI 方式運行
- 管理你的應用程序
- 處理多環境
- 在視圖文件中使用 PHP 替代語法
- 安全
- PHP 開發規范
- 類庫參考
- 基準測試類
- 緩存驅動器
- 日歷類
- 購物車類
- 配置類
- Email 類
- 加密類
- 加密類(新版)
- 文件上傳類
- 表單驗證類
- FTP 類
- 圖像處理類
- 輸入類
- Javascript 類
- 語言類
- 加載器類
- 遷移類
- 輸出類
- 分頁類
- 模板解析類
- 安全類
- Session 類
- HTML 表格類
- 引用通告類
- 排版類
- 單元測試類
- URI 類
- 用戶代理類
- XML-RPC 與 XML-RPC 服務器類
- Zip 編碼類
- 數據庫參考
- 數據庫快速入門: 示例代碼
- 數據庫配置
- 連接你的數據庫
- 查詢
- 生成查詢結果
- 查詢輔助函數
- 查詢構造器類
- 事務
- 數據庫元數據
- 自定義函數調用
- 數據庫緩存類
- 數據庫工廠類
- 數據庫工具類
- 數據庫驅動器參考
- 輔助函數參考
- 數組輔助函數
- 驗證碼輔助函數
- Cookie 輔助函數
- 日期輔助函數
- 目錄輔助函數
- 下載輔助函數
- 郵件輔助函數
- 文件輔助函數
- 表單輔助函數
- HTML 輔助函數
- 語言輔助函數
- Inflector 輔助函數
- 數字輔助函數
- 路徑輔助函數
- 安全輔助函數
- 表情輔助函數
- 字符串輔助函數
- 文本輔助函數
- 排版輔助函數
- URL 輔助函數
- XML 輔助函數
- 向 CodeIgniter 貢獻你的力量