状态 草稿
Todo Proof read
官方最后更新时间 2009/01/28 03:37

URL 辅助函数

提供URL辅助方法。

方法

base()

url::base() 返回基 URL(在 config.php 文件中的 site_protocol 和 site_domain定义)

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
 
echo url::base();

显示结果:

http://localhost/kohana/

url::base() 接受两个可选参数。第一个参数如果你想附加 index_page (同样定义在 config.php 中)到基 URL 请设置为 TRUE;第二个参数可以覆写 config.php 中默认的 site_protocol。

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
// index_page = 'index.php'
 
echo url::base(TRUE, 'https');

显示结果:

https://localhost/kohana/index.php

site()

url::site() 返回定义在 config.php 中基于 site_protocol,site_domain,index_page,url_suffix 的 URL。

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
// index_page = 'index.php'
// url_suffix = '.html'
 
echo url::site();

显示结果:

http://localhost/kohana/index.php/.html

url::site() 接受两个可选参数。第一个参数如果你想附加 index_page (同样定义在 config.php 中)到基 URL 请设置为 TRUE;第二个参数可以覆写 config.php 中默认的 site_protocol。

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
// index_page = ''
// url_suffix = '.html'
 
echo url::site('admin/login', 'https');

显示结果:

https://localhost/kohana/admin/login.html

current()

url::current() 返回当前 URI 的字符串。这个方法接受一个参数。如果设置为 TRUE,请求的字符串将会包括在返回值中。

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
// index_page = 'index.php'
// url_suffix = '.html'
 
// 当前 URL: http://localhost/kohana/index.php/welcome/home.html?query=string
 
echo url::current();

返回结果

welcome/home

当可选参数设置为 TRUE

echo url::current(TRUE);

返回结果

welcome/home?query=string

title()

url::title() 返回适当格式化后的标题,比如在用到一个 URL,第一参数是输入标题字符串(强制性的),而第二个可选参数是用在设置特殊字符。默认情况下,是破折号。你可以根据自己需要修改。

$input_title = ' __Ecléçtic__ title\'s  entered by cràzed users- ?>  ';
 
echo url::title($input_title, '_');

返回结果:

eclectic_titles_entered_by_crazed_users

标题发生了什么事情?所有非字母,数字字符,除破折号或下划线(取决于第二个参数)将被删除。不过,非 ASCII 字符则会首先被音译(例如,à 直译为 a)以保证标题即可得的读出来。最终 URL 的标题还会被转换为小写字符。

redirect()

url::redirect() 生成 HTTP 服务器头信息(302)并执行 system.redirect 事件,这将浏览器重定向到指定的 URL 上面,默认的 site_domain 定义在 config.php

url::redirect() 始终调用 php 的 exit 函数以防止脚本继续执行

url::redirect('http://www.whitehouse.gov');

跳转到 White House 网站。

可选的第二参数用在设置跳转方法。默认是 302。

// site_domain = 'localhost/kohana/'
// site_protocol = 'http'
 
url::redirect('aboutus', 301);

使用 301 头信息跳转到 http://localhost/kohana/aboutus.

如果你想发送多个选择(300)的跳转,则需要使用 URI 的数组形式:

url::redirect(array('aboutus','http://www.kohana.php/'), 300);

在数组中的第一个 URI 被认为是首选的 URI 并将放置在 Location header。所有的 URI 都会输出一个 HTML 无序列表。一般来说,浏览器将依据Location header 且这个列表永远不会被看到。然而,它并没有为 user agent 应该做的接到了 300 以上的定义标准。列表可根据你的提供用于可用于当前用户的选择。

helpers/url.txt · 最后更改: 2009/02/02 23:55 由 icyleaf