FuelPHP 请求和响应

fuelphp 请求和响应

 

http 请求和 http 响应在任何 web 应用程序中都扮演着重要的角色。我们需要获取 http 请求的完整详细信息才能正确处理它。处理完后,我们需要通过http响应将处理后的数据发送给客户端。

fuelphp 提供了优秀的 request 和 response 类来分别读写http 请求和http 响应。让我们学习本章中的 request 和 response 类。

 

请求

在典型的 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 教程

    相关文章