使用CakePHP创建REST API [英] Creating REST API with CakePHP

查看:145
本文介绍了使用CakePHP创建REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用CakePHP为移动后端创建REST API。按照此处的说明进行操作之后,我已经能够设置创建条件REST API。问题是当我使用 _serialize 变量时,我的输出中得到了 null 。这是我的代码:

I have to create a REST API for mobile backend using CakePHP. After following the instructions here I have been able to setup the requirements for creating a REST API. The problem is that I am getting a null in my output when I use the _serialize variable. Here is my code:

class InvitationController extends AppController {

    public $components = array('RequestHandler');

    public function index() {

    }

    public function add() {

        if ($this->request->is('post')) {
            $this->loadModel('Organizer');

            $numPeople = $this->request->data['numpeople'];
            $share = $this->request->data['myshare'];
            $organizer = $this->request->data['organizer'];     
            $organizerMobile = $this->request->data['organizermobile'];
            $this->set('organizerMobile', $organizerMobile);    
            $deadline = $this->request->data['deadline'];

            $links = array();       

            date_default_timezone_set("Asia/Calcutta");
            $now = date('Y-m-d');
            $lastOrganizer = $this->Organizer->find('first', array(
                'order' => array('Organizer.id' => 'desc')
            ));

            $checkOrganizer = $this->getOrganizerDetails($lastOrganizer);

            $pass = //something;


            // save data in organizers table
            $this->organizer->create();
            $this->organizer->save(
                array(
                    'name' => $organizer,
                    'mobile' => $organizerMobile,
                    'p_id' => 1,
                    'share' => $share,
                    'deadline' => $deadline,
                    'password' => $pass,
                    'group_cardinality' => $numPeople,
                    'created_on' => $now,
                )
            );

            $message = 1;
            $this->set(array(
                'message' => $message,
                '_serialize' => array($message)
            ));
        }
    }
}   

当我向 POST /invitation.json 我在输出中得到 null 的状态为 200 。另一方面,如果我只是在 Controller 或<$ c $中执行 echo json_encode($ message) c>查看我得到正确的输出。我想我做错了。请帮忙!

When I make a request to POST /invitation.json I get null in the ouput with status 200. On the other hand if I simply do echo json_encode($message) in the Controller or in the View I get the correct output. I think I am doing something wrong. Please help!

推荐答案

错了:

 '_serialize' => array($message)

请注意手册,在提供的示例中正确显示了该手册。

Pay attention to the manual, it shows it correctly in the provided examples.

 '_serialize' => array('message')

这篇关于使用CakePHP创建REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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