简体   繁体   English

使用CakePHP创建REST API

[英]Creating REST API with CakePHP

I have to create a REST API for mobile backend using CakePHP. 我必须使用CakePHP为移动后端创建REST API。 After following the instructions here I have been able to setup the requirements for creating a REST API. 按照此处的说明进行操作后我就可以设置创建REST API的要求。 The problem is that I am getting a null in my output when I use the _serialize variable. 问题是,当我使用_serialize变量时,输出中的null 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)
            ));
        }
    }
}   

When I make a request to POST /invitation.json I get null in the ouput with status 200 . 当我请求POST /invitation.json时,输出中的状态为200 null On the other hand if I simply do echo json_encode($message) in the Controller or in the View I get the correct output. 另一方面,如果我只是简单地在ControllerView echo json_encode($message) ,则会得到正确的输出。 I think I am doing something wrong. 我想我做错了。 Please help! 请帮忙!

That's wrong: 错了:

 '_serialize' => array($message)

Pay attention to the manual , it shows it correctly in the provided examples. 请注意本手册 ,它在所提供的示例中正确显示。

 '_serialize' => array('message')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM