首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

CakePHP2 Request请求对象汉语教程

2012-12-20 
CakePHP2 Request请求对象中文教程CakeRequest是CakePHP默认的请求对象。该类用于对请求数据的处理与交互。

CakePHP2 Request请求对象中文教程

CakeRequest是CakePHP默认的请求对象。该类用于对请求数据的处理与交互。在每一次请求过程中,CakeRequest对象都会被创建,并通过引用的方式传递到应用程序需要使用这些数据的层中(如控制器,视图)。默认的,CakeRequest对象被赋予$this->request,可以在控制器,视图和助手类中使用。通过控制器引用的方式,也可以在组件类中使用请求对象。总的来说,CakeRequest对象主要负责以下几个功能:

  • 处理GET,POST,FILES数组,并以对象形式返回这些数据
  • 提供发起请求的客户端相关信息,如headers,客户端IP地址,域名信息
  • 提供获取请求参数的方法,包括数组及对象属性。

    获取请求参数

    CakeRequest提供了多个接口用于获取请求参数。第一种方式是通过数组索引的形式,第二种通过$this->request-params,第三种通过对象属性的形式。例如获取当前请求的控制器。

    }));

    框架默认的环境检测方式如下,

    • is(‘get’) Check to see if the current request is a GET.
    • is(‘put’) Check to see if the current request is a PUT.
    • is(‘post’) Check to see if the current request is a POST.
    • is(‘delete’) Check to see if the current request is a DELETE.
    • is(‘head’) Check to see if the current request is HEAD.
    • is(‘options’) Check to see if the current request is OPTIONS.
    • is(‘ajax’) Check to see of the current request came with X-Requested-with = XmlHttpRequest.
    • is(‘ssl’) Check to see if the request is via SSL
    • is(‘flash’) Check to see if the request has a User-Agent of Flash
    • is(‘mobile’) Check to see if the request came from a common list of mobile agents.

      Interacting with other aspects of the request

      这里再一次讲CakeRequest中关于路径、当前URL地址的一些方法和属性做下比较。

      • $this->request->webroot 包含了当前根目录的路径
      • $this->request->base 相当于PHP函数中获取的base path
      • $this->request->here 获取当前请求的完整路径
      • $this->request->query 包含了查询字符串参数

        更多关于CakePHP的CakeRequest对象的属性及方法,可以参考官方提供的CakeRequest API。

热点排行