CakeRequestオブジェクトの拡張

I have a web application written in CakePHP that needs to read request data from a JSON payload as opposed to standard application/x-www-form-urlencoded data. I would like to be able to access this data via the standard $this->request->data methodology. Is there a supported way to extend the CakeRequest object so that it is able to accept requests in this format?

1

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'))));

気をつけて、あなたがしていることと幸運を知っていることを確認してください。

1
追加された