【问题标题】:Routing to JSON responses in CakePHP在 CakePHP 中路由到 JSON 响应
【发布时间】:2014-03-17 20:56:43
【问题描述】:

在 CakePhp 项目中,我有一个带有控制器的项目模型:

class ItemsController extends AppController{
  public $components = array('RequestHandler');

  public function data(){
    $items = $this->Item->find('all');
    $this->set('items', $items);
    $this->set('_serialize', array('items'));
  }
}

通过在routes.php 中指定:

Router::parseExtensions('json');

/items/data.json 发出请求时,我得到了我想要的 JSON 响应

调用 /items/data 时如何获得 JSON 响应?以及如何在路由中连接 JSON 响应。例如,如果我希望 /data/items.php 呈现与 /items/data.json 请求相同的 JSON。

谢谢!

【问题讨论】:

    标签: php json cakephp cakephp-2.4


    【解决方案1】:

    我已经在前置过滤器中使用 RequestHandler 的一些方法解决了

    public function beforeFilter() {
      parent::beforeFilter();
      $this->RequestHandler->setContent('json');
      $this->RequestHandler->renderAs($this, 'json');
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2013-05-17
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 2020-12-07
      • 2018-08-23
      相关资源
      最近更新 更多