Request 請(qǐng)求類
?? Request 構(gòu)造函數(shù)已私有,無法通過 new 方式實(shí)例化,僅通過 控制器 的
getRequest
方法獲得
// in controller-action
$request = $this->getRequest();
if ($request->isPost()) {
// todo if post
}
Request::getUri
獲取當(dāng)前請(qǐng)求 Uri (即純路徑部分,不包含 query)
string Request::getUri ( void )
參數(shù)
無返回值
Uri 字符串范例
$uri = $request->getUri(); // 如 foo/bar
Request::getRequestUri
獲取當(dāng)前請(qǐng)求 RequestUri (包含路徑部分和 query 部分)
string Request::getRequestUri ( void )
參數(shù)
無返回值
RequestUri 字符串范例
$requestUri = $request->getRequestUri(); // 如 foo/bar?hello=world
Request::getBaseUri
獲取當(dāng)前請(qǐng)求路徑前綴,即相對(duì)于 DocumentRoot 的路徑,通常為 /
string Request::getBaseUri ( void )
參數(shù)
無返回值
BaseUri 字符串范例
$baseUri = $request->BaseUri(); // 如 /path/to/index/
Request::isPost
判斷是否為 POST 請(qǐng)求
bool Request::isPost ( void )
參數(shù)
無返回值
POST 請(qǐng)求返回true
,否則返回false
范例
if ($request->isPost()) {
// todo if post
}
Request::isAjax
判斷是否為 Ajax 請(qǐng)求 (XMLHttpRequest)
bool Request::isAjax ( void )
參數(shù)
無返回值
Ajax 請(qǐng)求返回true
,否則返回false
范例
if ($request->isAjax()) {
// todo if ajax
}
Request::getQuery
獲取 query 即 $_GET
參數(shù)值
string Request::getQuery ( string $key [, mixed $default = null] )
不建議直接使用
$_GET
超全局變量
參數(shù)
$key - query 鍵名
$default - 如果鍵名不存在,則返回該默認(rèn)值,默認(rèn)為null
返回值
query 值范例
// 請(qǐng)求為 /foo/bar?hello=world
$hello = $request->getQuery('hello', false); // 返回 "world"
Request::getQueryTrim
獲取 query 參數(shù)值,并清除前后空格
同 Request::getQuery
,只是獲取結(jié)果為字符串類型時(shí),執(zhí)行 trim()
方法清除前后空格
Request::getPost
獲取 $_POST
參數(shù)值
string Request::getPost ( string $key [, mixed $default = null] )
不建議直接使用
$_POST
超全局變量
參數(shù)
$key - post 鍵名
$default - 如果鍵名不存在,則返回該默認(rèn)值,默認(rèn)為null
返回值
post 值范例
if ($request->isPost()) {
$foo = $request->getPost('foo', false); // 若 $_POST['foo'] 存在則返回值,否則返回 false
}
Request::getPostTrim
獲取 $_POST
參數(shù)值,并清除前后空格
同 Request::getPost
,只是獲取結(jié)果為字符串類型時(shí),執(zhí)行 trim()
方法清除前后空格
Request::getCookie
獲取 $_COOKIE
參數(shù)值
string Request::getCookie ( string $key [, mixed $default = null] )
不建議直接使用
$_COOKIE
超全局變量
參數(shù)
$key - cookie 鍵名
$default - 如果鍵名不存在,則返回該默認(rèn)值,默認(rèn)為null
返回值
cookie 值范例
$foo = $request->getCookie('foo', false); // 若 $_COOKIE['foo'] 存在則返回值,否則返回 false