【问题标题】:Zend/PHP: how to perform HTTP POST to controller and mapper php classesZend/PHP:如何对控制器和映射器 php 类执行 HTTP POST
【发布时间】:2016-03-24 13:38:10
【问题描述】:

我们的 Android 应用程序应该向运行 PHP Zend 的服务器执行 POST 请求,以便将数据保存在 MySQL 中。根据 Zend MVC,我们的老板告诉我们实现以下类:

GcmUsersController.php

class GcmUsersController extends AppController
{
 public function registationAction()
  {
   if($this->getRequest()->isPost()){
      $token = $this->getRequest()->getPost('token');
      $android_id = $this->getRequest()->getPost('android_id');
      $name = $this->getRequest()->getPost('name');
      $gcm = new Application_Model_GcmUsersMapper();
      $info = array(
          "tokenid" => $token,
          "gcm_regid" => $android_id,
          "name" => $name,
          "email" => "a@a.it"
      );
      $gcm->insert2($info, "GcmUsers");
   }
}
?>

GcmUsersMapper.php

class Application_Model_GcmUsersMapper extends Application_Model_AppMapper
{

  protected $_mvcTable = 'GcmUsers';

  protected $_dbTable = 'gcm_users';
}

我们的应用程序 Android 应该执行 POST HTTP 的 url 是什么?这就是我们所做的:

final HttpPost httppost = new HttpPost("http://mypath/GcmUsers/registration");

因为我们认为 URL 中的注册和控制器中的注册操作之间存在绑定,但它不起作用。哪种方法正确?

【问题讨论】:

  • 你的 url 应该包含 php 脚本文件。所以GcmUsersController.php.

标签: java php android mysql zend-framework


【解决方案1】:

解决方案是这样的:

内部视图->脚本创建目录apnsdevices 并将文件registrationapple.phtml 插入其中:

<?php 
  echo $this->data;
?>

那么,这里是ApnsDevicesMapper.php

<?php 
class Application_Model_ApnsDevicesMapper extends Application_Model_AppMapper
{

protected $_mvcTable = 'ApnsDevices';

protected $_dbTable = 'apns_devices';

public function registration($device_id, $token) {
    $where = "deviceuid = '".$device_id."'";
    $c = $this->count($this->_mvcTable,$this->_dbTable,$where);
    if($c == 0){
        $info = array(
            "devicetoken" => $token,
            "deviceuid" => $device_id
        );
        $this->insert2($info, "ApnsDevices");

    }
    return $c;   
}


}
?>

这是ApnsdevicesController.php

<?php
class ApnsdevicesController extends AppController
{
public function registrationappleAction()
{
    if($this->getRequest()->isPost()){
        $token = $this->getRequest()->getPost('token');
        $device_id = $this->getRequest()->getPost('device_id');
        $apns = new Application_Model_ApnsDevicesMapper();
        $c = $apns->registration($device_id, $token);
        $this->view->data = $c;
    }
}

}

?>

最后,我可以对以下 URL 执行 POST 方法:

http://server_path/apnsdevices/registrationapple

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 2012-12-13
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多