成免费的crm,久久国产精品新农夫导航新妓网,恋夜秀场全部视频安卓手机,女校花强奷在线播放A级

FuelPHP 請求和響應

fuelphp 請求和響應

 

http 請求和 http 響應在任何 web 應用程序中都扮演著重要的角色。我們需要獲取 http 請求的完整詳細信息才能正確處理它。處理完后,我們需要通過http響應將處理后的數據發送給客戶端。

fuelphp 提供了優秀的 requestresponse 類來分別讀寫http 請求和http 響應。讓我們學習本章中的 requestresponse 類。

 

請求

在典型的 web 應用程序中,應用程序需要解析當前請求的詳細信息。 request 類提供了簡單的方法來解析應用程序要處理的當前請求。 request 還提供了一個選項,通過充當 http 客戶端來創建新請求。

創建新請求使應用程序能夠請求應用程序的其他部分 or 完全是另一個應用程序并顯示結果。讓我們在本章中學習如何解析傳入的請求,并在 hmvc 請求章節中學習如何創建新請求。

 

解析請求

request 類提供了三種方法來獲取 http 請求的詳細信息。它們如下,

active-它是一個靜態方法,返回當前活動的 http 請求。

$currentrequest = request::active();

param-它返回指定參數的值。它包含兩個參數。第一個參數是參數名稱,第二個參數是要返回的值,如果該參數在當前 http 請求中不可用。

$param = request::active()->param('employee_name', 'none');

params-除了將所有參數作為數組返回之外,它與 param 相同。

$params = request::active()->params();

 

示例

讓我們創建一個簡單的表單并使用請求類處理表單。

步驟 1-在員工控制器中創建一個新操作 action_request。

public function action_request() {
}

步驟 2-調用請求方法以獲取當前請求的所有參數。

public function action_request() {
   $params = request::active()->params();
}

步驟 3-轉儲獲取的參數數組。

public function action_request() {
   $params = request::active()->params();
   echo dump($params);
}

step 4-更改路由以在路由配置文件中包含參數, fuel/app/config/routes.php

'employee/request(/:name)?' => array('employee/request', 'name' => 'name'),

現在,請求新操作 http://localhost:8080/employee/request/jon,它將顯示以下響應。

 

回復

response 類提供了創建 http 響應的選項。默認情況下,我們在大多數情況下不需要直接使用響應類。相反,我們使用 view(我們將在下一章學習)來創建 http 響應。 view 對開發人員隱藏 http 響應,并使用底層 response 類將響應發送給客戶端。在高級的情況下,我們直接使用response類,創建一個完整的http響應。

 

創建響應

響應由標題和正文組成。主要標頭是 http 狀態代碼。 http 狀態碼是 http 協議中定義的用于描述響應的標準代碼。例如狀態碼,200表示請求成功。

response 類提供了三個參數來創建 http 響應,

  • $body-http 響應的正文
  • $status_code-http 響應的狀態代碼
  • $headers-作為數組的可選標題
  • $body = "hi, fuelphp";
    $headers = array (
       'content-type' => 'text/html',
    );
    $response = new response($body, 200, $headers);

    讓我們在員工控制器中創建一個新動作, action_response,如下所示。

    public function action_response() {
       $body = "hi, fuelphp";
       $headers = array (
          'content-type' => 'text/html',
       );
       $response = new response($body, 200, $headers);
       return $response;
    }

     

    結果

     

    方法

    response 類提供了很多操作 http 響應的方法。它們如下,

    forge-它與上面看到的響應類構造函數相同。

    return response::forge("hi, fuelphp", 404);

    redirect-它提供了重定向到 url 而不是發送響應的選項。它包含以下參數,

    a.url-目標網址 b.方法-重定向方法。 location(默認)和 refresh c.redirect_code-http 狀態代碼。默認值為 302、

    // use a url
    response::redirect('http://some-domain/index', 'refresh');
    // or use a relative uri
    response::redirect('employee/list');

    redirect_back-除了重定向到上一頁之外,它類似于重定向方法。如果沒有可用的后臺頁面,我們可以指定重定向頁面。

    // if there is no back page, go to the employee list page
    response::redirect_back('/employee/list', 'refresh');

    set_status-它提供了一個設置 http 狀態代碼的選項。

    $response = new response();
    $response->set_status(404);

    set_header-它提供了一個設置 http 標頭的選項。

    $response = new response();
    $response->set_header('content-type', 'application/pdf');
    // replace previous value using third arguments
    $response->set_header('content-type', 'application/pdf', 'text/plain');

    set_headers-與 set_header 相同,只是它提供了使用數組設置多個標題的選項。

    $response = new response();
    $response->set_headers(array
       'content-type' => 'application/pdf',
       'pragma' => 'no-cache',
    ));

    get_header-它可以獲取先前設置的標題詳細信息。

    $response = new response();
    $response->set_header('pragma', 'no-cache');
    // returns 'no-cache'
    $header = $response->get_header('pragma');
    // returns array('pragma' => 'no-cache')
    $header = $response->get_header();

    body-它提供了一個選項來設置 http 響應的正文。

    $response = new response();
    $response->body('hi, fuelphp');
    // returns 'hi, fuelphp'
    $body = $response->body();

    send_headers-將標頭發送到請求的客戶端。 fuelphp 使用此方法將響應發送到客戶端。通常情況下,我們不需要使用這種方法。

    $response->send_headers();

    send-與 send_headers 相同,但 http 響應中可能會限制標頭。

    // send the headers as well
    $response->send(true);
    // only send the body
    $response->send(false);
    $response->send();

    下一節:fuelphp 視圖

    fuelphp 教程

    相關文章