当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

Uri - 类別

优质
小牛编辑
134浏览
2023-12-01

Uri 类别能让你与 URI 互动。

base($include_index = true)

base 方法回传基础 URL,不包括 index_file 当 $include_index 设为 false 时。

请注意:如果在 app/config/config.php 里 index_file 没有被设定,$include_index 将被忽略。

静态
参数
参数预设描述
$include_index
true
是否附加索引到回传 URL。
回传字串,基础 URL 与选择性的索引档案附加
範例
echo Uri::base();
// http://localhost/index.php

echo Uri::base(false);
// http://localhost/

create($uri = null, $variables = array(), $get_variables = array(), $secure = null)

create 方法能让你建立一个 URL 根据所给的 URI,包括基础 URL。

静态
参数
参数预设描述
$uri
null
URL。
$variables
array()
URL 的变数。
$get_variables
array()
添加到 URL 的查询字串,其值 & 键可使用 $variables 阵列中的变数。
$secure
null
设定为 false 以强制 http,或设定为 true 以强制 https 在建立的 URL
回传字串
範例
echo Uri::create('controller/method');
// 回传 http://localhost/controller/method

echo Uri::create('controller/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'));
// 回传 http://localhost/controller/thing?what=more

echo Uri::create('http://www.example.org/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'));
// 回传 http://www.example.org/thing?what=more

echo Uri::create('http://www.example.org/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'), true);
// 回传 https://www.example.org/thing?what=more

current()

current 方法能让你提取目前的 URL,包括在 HMVC 模式的基础 URL。

静态
参数
回传字串
範例
// 範例 URL:http://localhost/module/controller/method
echo Uri::current(); // 回传 http://localhost/module/controller/method

main()

main 方法能让你提取目前的 URL,包括基础 URL。

静态
参数
回传字串
範例
// 範例 URL:http://localhost/controller/method
echo Uri::main(); // 回传 http://localhost/controller/method

segment($segment, $default = null)

segment 方法能让你回传想要的分段,或不存在时回传 false。

静态
参数
参数预设描述
$segment必要分段编号。
$default
null
预设值。
回传字串
範例
// 範例 URL:http://localhost/controller/method/param1/param2
echo Uri::segment(3); // 回传 param1

segments()

segments 方法能让你提取目前的 URI 分段阵列。

静态
参数
回传阵列
範例
// 範例 URL:http://localhost/controller/method
echo Uri::segments(); // 回传 array(0 => 'controller', 1 => 'method')

segment_replace($url, $secure = null)

segment_replace 方法能让你以目前的 URI 分段,在所给的 URL 中取代该位置的万用字元。

静态
参数
参数预设描述
$url必要包含万用字元的 URL。万用字元被定义为一个 * 字元。
$secure
null
如果为 false,回传的 URL 会使用 HTTP,如果为 true,它会使用 HTTPS。
回传字串
抛出OutOfBoundsException,如果一个万用字元的位置在片段中不存在任何值。
範例
// 範例:目前 URL = http://localhost/one/two,有片段 'one' 跟 'two'

// 回传 http://localhost/one/two/three
echo Uri::segment_replace('http://localhost/*/*/three');

// 抛出一个例外,因为没有可用的第三片段
echo Uri::segment_replace('http://localhost/this/that/*/other');

// 回传 https://localhost/one/two/three
echo Uri::segment_replace('http://localhost/*/*/three', true);

update_query_string($vars = array(), $uri = null, $secure = null)

update_query_string 方法能让你更新目前或传递的 URI 的查询字串。

静态
参数
参数预设描述
$vars
array()
一个查询变数的关联阵列,或一个包含变数名称的字串。
$uri
null
要使用的 URL。如果没给,目前的 URL 会被使用。如果 $vars 是一个字串,这会包含变数值 (这意味着在这情况下,你不能传递一个自订的 URI)。
$secure
null
如果为 false,回传的 URL 会使用 HTTP,如果为 true,它会使用 HTTPS。
回传字串
範例
// 例如:Current URL = http://localhost/test?one=1&two=2

// 回传 http://localhost/test?one=1&two=2&three=3
echo Uri::update_query_string(array('three' => 3));

// 回传 http://localhost/test?one=1&two=3
echo Uri::update_query_string(array('two' => 3));

// 回传 http://localhost/controller?four=4
echo Uri::update_query_string(array('four' => 4), 'http://localhost/controller');

// 回传 https://localhost/test?one=1&two=2&three=3
echo Uri::update_query_string('three', 3, true);

to_assoc($start = 1)

to_assoc 方法使用分段阵列并把它变成一个关联阵列如果分段数是偶数。如果分段数是奇数,最后的阵列键将有一个 null 值。

静态
参数
参数预设描述
$start
1
要开始的分段数。这能让你剥离引导分段。
回传阵列
範例
// Uri 是 /welcome/index/page/4
$arr = Uri::to_assoc();
/*
结果:
array(
    'welcome' => 'index',
    'page'    => 4,
);
*/

// Uri 是 /welcome
$arr = Uri::to_assoc();
/*
结果:
array(
    'welcome' => null,
);
*/

string()

string 方法能让你提取目前的 URI。

静态
参数
回传字串
範例
// 範例 URL:http://localhost/controller/method
echo Uri::string(); // 回传 controller/method

build_query_string($values ...)

build_query_string 方法能让你从一个或更多传递的关联阵列或字串值产生一个查询字串。 你可以透过 Input::get() 从现有的来建构一个更新的查询字串。如果你传递一个字串,它会被假设为一个开关, 并且会被转换为 "string=1"。

静态
参数
参数预设描述
$values必要一个或更多关联阵列或字串值
回传字串
範例
// 回传 "varA=varA&varB=1&varC=varC"
echo Uri::build_query_string(array('varA' => 'varA'), 'varB', array('varC' => 'varC'));