PHP头条
热点:

CakePHP2Request请求对象中文教程



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

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

获取请求参数

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

01 02 03 $this->request['controller']; $this->request->controller; $this->request->params['controller']

上述的三种方式都能获取到当前请求的控制器名。通过提供多种方式来获取请求参数,可以为应用程序提供移植、升级的各种便利。a,不仅控制器可以通过如上方式获取,所有的路由元素均可以通过这些接口获取到。

除了路由元素之外,b,最常使用的请求参数还有URL中的普通参数(Passed arguments)和命名参数()。这些参数同样可以通过请求对象的三个接口获取。

01 02 03 04 05 06 07 08 09 //passed arguments $this->request['pass'] $this->request->pass $this->request->params['pass']   //named parameters $this->request['named'] $this->request->named $this->request->params['named']

上面同样通过CakeRequest的三个接口获取到了普通参数和命名参数。c,需要注意的是,CakePHP有很多非常重要,同时非常有用的参数,这些参数都能够通过CakeRequest对象的请求参数中获取。

  • plugin 处理当前请求的插件,没有则返回null
  • controller 处理当前请求的控制器
  • action 处理当前请求的控制器方法
  • prefix 当前控制器的前缀。如admin_edit。在路由中配置。
  • bare 通过requestAction()发起的请求包含的参数。
  • requested 当请求来自requestAction()时,值为true。

获取查询字符串参数 Accessing Querystring parameters

这个在未做重写的PHP程序中是最为常见的URL形式,可以通过CakeRequest::$query获取到查询字符串参数。

01 02 03 // URL地址 /posts/index?page=1&sort=title $this->request->query['page'] $this->request['url']['page']

获取POST数据 Accessing POST data

所有通过POST形式传递的数据都可以通过CakeRequest::$data获取。所有表单中包含data前缀的域的数据,都可以通过移除data字符串,然后通过CakeRequest::$data获取到该值。例如。

01 02 //当一个表单域的name属性为data[Post][title]时,该值可以在提交的控制器中通过如下方式获取 $this-request->data['Post']['title'];

获取路径信息 Accessing path information

CakeRequest提供了关于当前应用程序的路径信息。CakeRequest::$base和CakeRequest::$webroot用于生成url地址相当不错,另外它们还能够自动判断当前程序是否在子目录当中。(这两个方法对于子目录的判断我没有使用过,以后会在这里补充更详细的使用方法)。

检查请求 Inspecting the request

检查不同的请求环境,在2.0版本之前,一般通过RequestHandlerComponent组建进行。新版本的CakePHP已经将这些方法统一到CakeRequest请求对象当中,并且提供了一个新的接口用于向后兼容。

01 02 $this->request->is('post'); $this->request->isPost();

上述两种方式都可以检测当前请求是否通过POST发起。这里有扩展用于创建新的方式检测请求环境,如下所示。

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 // Add an environment detector. $this->request->addDetector('post',array('env'=>'REQUEST_METHOD','value'=>'POST'));   // Add a pattern value detector. $this->request->addDetector('iphone',array('env'=>'HTTP_USER_AGENT','pattern'=>'/iPhone/i'));   // Add an option detector $this->request->addDetector('internalIp',array(     'env'=>'CLIENT_IP',     'options'=>array('192.168.0.101','192.168.0.100') ));   // Add a callback detector. Can either be an anonymous function or a regular callable. $this->request->addDetector('awesome',array('callback'=>function($request) {     returnisset($request->awesome); }));

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

  • 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。

www.phpzy.comtrue/phpkj/11194.htmlTechArticleCakePHP2Request请求对象中文教程 CakeRequest是 CakePHP 默认的请求对象。该类用于对请求数据的处理与交互。在每一次请求过程中,CakeRequest对象都会被创建,并通过引用的方式传递到应用程...

相关文章

相关频道:

PHP之友评论

今天推荐