CakePHP应用程序作为移动应用程序的后端

CakePHP应用程序作为移动应用程序的后端,cakephp,cakephp-2.3,Cakephp,Cakephp 2.3,我一直在为浏览器开发这个cakePHP应用程序,但现在需要支持一些为android和iOS应用程序服务的功能,我无法获得如何让它运行的可靠信息。例如,这是必须支持移动应用程序的功能之一: function add() { if (!empty($this->data) ) { $this->Customer->create(); if ($this->Customer->save($this->data)) {

我一直在为浏览器开发这个cakePHP应用程序,但现在需要支持一些为android和iOS应用程序服务的功能,我无法获得如何让它运行的可靠信息。例如,这是必须支持移动应用程序的功能之一:

function add() {

    if (!empty($this->data) ) {
        $this->Customer->create();
        if ($this->Customer->save($this->data)) {
            $this->Session->setFlash(__('Customer is saved'), 'positive_notification');
            $this->redirect(array('controller'=>'customers', 'action' => 'index'));
        } else {
            $this->Session->setFlash(__('Customer was not saved. Please try again'), 'negative_notification');
        }
    }
}
因此,据我所知,调用函数可以通过应用程序的POST来完成。如果我错了,请更正我。但是,根据设备的不同,应该给出不同的返回

还是为不同的客户机公开不同的功能更好


欢迎提供任何帮助、指导或建议。

根据您发布的功能:

该函数将根据收到的数据保存客户,并返回响应 您将无法使用$this->redirect或setFlash 您可能会返回JSON响应。 函数可能看起来像

public function add() {
     $response = array(
        'status' => false,
        'message' => __('Customer was not saved. Please try again')
    );

    if (!empty($this->data)) {
        $this->Customer->create();
        if ($this->Customer->save($this->data)) {
            $response = array(
               'status' => true,
               'message' => __('Customer is saved')
            );
        }
    }
    $this->set(array(
        'response' => $response ,
        '_serialize' => array('response')
    ));
}
如果您希望在网站和API中都具有相同的CakePHP应用程序功能,那么可以为每个应用程序使用不同的控制器:网站的客户控制器和API APICustomer控制器,用于工作方式不同的API

根据需要,还可以有两个共享模型的CakePHP应用程序


关于如何制作一个由CakePhp支持的API,有很多参考资料,比如或

您需要返回到哪种设备类型以及为什么?就目前而言,你的问题还不清楚。基本上,我需要一个建议,告诉我应该采取什么样的方式来支持移动应用程序。休息完了吗?还是有别的办法?对于不同的回报应该有不同的行动,或者可以是双腿的。为什么你对待移动应用会有任何不同?是什么使他们的HTTP请求不同于来自任何其他用户代理的HTTP请求?如果不知道你到底需要做什么以及为什么要做,就不可能正确地回答你的问题。谢谢,这就是我所需要的。希望这对将来的人也有帮助。