扩展CakeRequest对象

时间:2011-12-18 20:22:53

标签: cakephp cakephp-2.0

我有一个用CakePHP编写的Web应用程序,它需要从JSON有效负载读取请求数据,而不是标准的application / x-www-form-urlencoded数据。我希望能够通过标准的$ this-> request->数据方法访问这些数据。是否有一种受支持的方式来扩展CakeRequest对象,以便它能够接受这种格式的请求?

1 个答案:

答案 0 :(得分:1)

以下是如何自定义CakeRequest对象的功能:

将以下内容插入app / Config / bootstrap.php:

/**
 * Enable customization of the request object.  Ideas include:
 *   * Accepting data in formats other than x-www-form-urlencoded.
 */
require APP . 'Lib' . DS . 'Network' . DS . 'AppCakeRequest.php';

创建app / Lib / Network,并添加AppCakeRequest.php:

<?php
/**
* AppCakeRequest
*
* Allows for custom handling of requests made to the application.
*/

class AppCakeRequest extends CakeRequest {
    // Do your magic, and be careful...
}

编辑app / webroot / index.php:

$Dispatcher->dispatch(new AppCakeRequest(), new CakeResponse(array('charset' => Configure::read('App.encoding'))));

小心,确保你知道自己在做什么,祝你好运。

相关问题