【问题标题】:Extending the CakeRequest Object扩展 CakeRequest 对象
【发布时间】:2011-12-22 00:52:36
【问题描述】:

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

【问题讨论】:

    标签: cakephp cakephp-2.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'))));
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 2017-11-21
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多