2011-12-18 158 views
1

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

回答

1

这里是你如何自定义CakeRequest对象的功能:

将以下到应用程序/配置/ 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'; 

创建应用程序/库/网络,并添加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')))); 

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