Azalea\Request

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容