CakePHP REST Plugin不返回数据 [英] CakePHP REST Plugin not returning data

查看:137
本文介绍了CakePHP REST Plugin不返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用REST插件进行调查: https://github.com/kvz / cakephp-rest-plugin 来提供一个API,我可以用它来开发一个PhoneGap应用程序。



我已经设置了如图所示的插件文档,但是我没有回到我的JSON数据...我看到一些问题提到同样的问题在这里: https://github.com/kvz/cakephp-rest-plugin/issues?state=closed 我已经尝试实现,但没有效果。



有没有人有这个问题?插件在一年没有更新,所以我想知道如果蛋糕的内部运作已经改变,导致它打破?



在任何情况下,这里是我的代码:

  // UsersController 
public $ components = array(
'Rest.Rest' & array(
'catchredir'=> true,
'actions'=> array(
'extract'=> array(
'test'=> array('users'),
),
),
'ratelimit'=> array(
'enable'=> false

),
);

public function test(){
$ users = array(
array('name'=>'user-1'),
array '=>'user-2'),
array('name'=>'user-3')
);
$ this-> set(compact('users'));
}

并在我的路线:

  Router :: mapResources(array('users')); 

Router :: parseExtensions('rss','json','xml');

,这里是返回的JSON:

  {
data:{
User:[]
},
meta:{
状态:ok,
feedback:[],
request:{
http_host:sample.com,
http_user_agent: Mozilla \ / 5.0(Macintosh; Intel Mac OS X 10_8_2)AppleWebKit \ / 537.22(KHTML,like Gecko)Chrome \ / 25.0.1364.29 Safari\ / 537.22,
server_addr:## 。##。###。###,
remote_addr:##。###。###。###,
server_protocol:HTTP\ /1.1,
request_method:GET,
request_uri:\ / users\ / test.json,
request_time:1357946870
},
credentials:{
class:null,
apikey:null,
username:null
},
time_epoch:1357946871,
time_local:Fri,11 Jan 2013 15:27:51 -0800,
version:0.3
} $ b b}


解决方案

Kevin的插件是为Cake 1.3建立一个应用程序与它一段时间)。它几乎已被过时的蛋糕2.x IMHO。



我回答了一个类似的问题,同时回答说明简化的REST Cake 2.x提供了以下功能:
使用CakePHP创建REST API p>

UPDATE:
在设置中检查解压缩数组。格式改变了最后1.3版本,但readme.md的一部分没有得到更新(即tweets示例是错误的)。请参阅警告 - 向后兼容性损坏部分,或者只需检查组件源代码 $ settings 数组以获取正确的格式。



正确的格式:

  public $ settings = array(
'actions'=> array b $ b'test'=> array(
'extract'=> array('users')


I'm looking into using the REST plugin here: https://github.com/kvz/cakephp-rest-plugin for providing an API that I can use to develop a PhoneGap application.

I've managed to setup the plugin as shown in the documentation, however I'm getting no data back in my JSON... I've seen some issues mentioning the same problem here: https://github.com/kvz/cakephp-rest-plugin/issues?state=closed which I have tried implementing but to no avail.

Has anyone had this issue themselves? The plugin hasn't been updated in a year, so I'm wondering if the inner-workings of Cake have changed causing it to break?

In any case here is my code:

// UsersController
public $components = array (
        'Rest.Rest' => array(
            'catchredir' => true,
            'actions' => array(
                'extract' => array(
                    'test' => array('users'),
                ),
            ),
            'ratelimit' => array(
                'enable' => false
            )
        ),
    );

public function test(){
        $users = array(
            array('name' => 'user-1'),
            array('name' => 'user-2'),
            array('name' => 'user-3')
        );
        $this->set(compact('users'));
    }

and in my routes:

Router::mapResources(array('users'));

Router::parseExtensions('rss','json','xml');

and here is the returned JSON:

{
    "data": {
        "User": []
    },
    "meta": {
        "status": "ok",
        "feedback": [],
        "request": {
            "http_host": "sample.com",
            "http_user_agent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit\/537.22 (KHTML, like Gecko) Chrome\/25.0.1364.29 Safari\/537.22",
            "server_addr": "##.##.###.###",
            "remote_addr": "##.###.###.###",
            "server_protocol": "HTTP\/1.1",
            "request_method": "GET",
            "request_uri": "\/users\/test.json",
            "request_time": 1357946870
        },
        "credentials": {
            "class": null,
            "apikey": null,
            "username": null
        },
        "time_epoch": "1357946871",
        "time_local": "Fri, 11 Jan 2013 15:27:51 -0800",
        "version": "0.3"
    }
}

解决方案

Kevin's plugin was built for Cake 1.3 (I built an app with it a while ago). It's pretty much been obsoleted by Cake 2.x IMHO. You should only use it if you need logging or rate-limiting or http authentication.

I answered a similar question a while back explaining the simplified REST Cake 2.x provides: Creating a REST API with CakePHP

UPDATE: Check your extract array in settings. The format changed with the last 1.3 version but part of readme.md didn't get updated (i.e. the tweets example is wrong). See section "Warning - Backwards compatibility breakage" or just check the component source code $settings array for the correct formats.

Correct format for you:

public $settings = array(
    'actions' => array(
        'test' => array(
            'extract' => array('users')

这篇关于CakePHP REST Plugin不返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆