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 状态码。

重要

该函数会终止脚本的执行。

相关文章
相关标签/搜索