CI URL 輔助函數

URL 輔助函數文件包含了一些幫助你處理 URL 的函數,在使用這些 函數以前首先要加載URL 輔助函數。php

該輔助函數經過下面的代碼加載:html

$this->load->helper('url');

  該輔助函數有下列可用函數:數組

1. 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 參數。ide

推薦在任什麼時候候都使用這種方法來生成你的 URL ,這樣在你的 URL 變更時你的代碼將具備可移植性。函數

傳給函數的 URI 段參數能夠是一個字符串,也能夠是個數組,下面是字符串的例子:codeigniter

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

上例將返回相似於:http://example.com/index.php/news/local/123post

下面是使用數組的例子:ui

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

 

2.  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() 函數相同,只是不會在 URL 的後面加上 index_page  url_suffix 

另外,和 site_url() 同樣的是,你也可使用字符串或數組格式的 URI 段。下面是字符串的例子:

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

上例將返回相似於:http://example.com/blog/post/123

 site_url() 函數不同的是,你能夠指定一個文件路徑(例如圖片或樣式文件),這將頗有用,例如:

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

將返回相似於:http://example.com/images/icons/edit.png

該函數是 CI_Config::base_url() 的別名,更多信息請查閱 配置類 文檔。

3. current_url()

返回: The current URL
返回類型: string

返回當前正在瀏覽的頁面的完整 URL (包括分段)。

註解

該函數和調用下面的代碼效果是同樣的: | | site_url(uri_string());

4.  uri_string()  

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

返回包含該函數的頁面的 URI 分段。例如,若是你的 URL 是:

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

函數將返回:

blog/comments/123

5. index_page()

返回: 'index_page' value
返回類型: mixed

返回你在配置文件中配置的 index_page 參數,例如:

echo index_page();

 6. 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() 函數同樣,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>

7. anchor_popup($uri = ''$title = ''$attributes = FALSE)

參數:
  • $uri (string) -- URI string
  • $title (string) -- Anchor title
  • $attributes (mixed) -- HTML attributes
返回:

Pop-up hyperlink

返回類型:

string

 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() <http://www.w3schools.com/jsref/met_win_open.asp> 函數的一個參數而已, 該函數接受一個窗口名稱或一個 window 對象。

註解

任何不一樣於上面列出來的其餘的屬性將會做爲 HTML 連接的屬性。

 

8.   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 狀態碼。

重要

該函數會終止腳本的執行。

相關文章
相關標籤/搜索