使用 javascript/jQuery 在 zend 模型中调用 php 方法的最佳方法是什么?


What's the best way to call a php method in zend model using javascript/jQuery

我正在开发一个网站,我必须与Web服务进行通信。我正在使用这样的 Ajax 调用,例如,如果我想调用一个名为 getCustomer(...我的Zend项目中有一个名为"客户"的模块。

在我的控制器内部,我有一个名为"jsonAction"的操作

public function jsonAction() {
        $this->_helper->viewRenderer->setNoRender();
        $server = new Zend_Json_Server();
        $server->setClass('Customer_Model_Customer');
        $server->handle();
        exit;
    }

它们在我的模型中具有以下功能:

/**
 * Get Customer
 * @param string $customerNr
 * @return object
 */
public function getCustomer($customerNr){
    /*....*/
}

所以我想使用 Ajax 调用调用这个函数。我所做的是这样的:

getCustomer : function(customerNr){
        if(customerNr == null)
            return;
        var request                 = {};
        request.method              = "getCustomer";
        request.params              = {};
        request.params.customerNr   = customerNr;
        request.id                  = Math.floor(Math.random()*101);
        postObject(webServicesURL, JSON.stringify(request), successGetCustomer);
    },

其中 postObject 是 Ajax 函数:

function postObject(url, request, successCallback){    
    try{
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: url,
            contentType: "application/x-www-form-urlencoded",
            data: request,
            xhrFields: {
                withCredentials: true
            },
            success: function(data){
                successCallback(data);
            },
            error:  function (data) {
                /*Notify error*/
            }
        });
    } catch(ex){
        /*erro*/
    }
}

我的问题是,还有另一种方法可以做到这一点?最好和优雅的方法?我是Web开发的新手,这就是我寻求您帮助的原因。

注意:我使用的是php Zend框架1.12,Ember 1.0和JQuery 1.8

更好的方法可能是不调用单个操作,而是调用不同的操作,并让contextswitch公开各种数据类型,您可以找到代码片段here

这是一种方法

//put this in your bootstrap
  protected function _initContextSwitch()
  {
    // Initialize contextSwitch helper
    Zend_Controller_Action_HelperBroker::addHelper(new Custom_Action_Helper_ContextSwitch());
  }
class Custom_Action_Helper_ContextSwitch extends
  Zend_Controller_Action_Helper_ContextSwitch
{
  public function preDispatch()
  {
      $actionName = $this->getActionController()->getRequest()->getActionName();
      $this
        ->addActionContext($actionName, 'json')
        ->initContext();
  }
}

现在扩展Zend_Controller_Action_Helper_ContextSwitch以使所有操作都公开 json 数据

现在调用每个带有查询字符串(format=json)的方法,就像http://example.com/module/controller/action?format=json这样,并将为每个操作公开json数据

注意:请记住,这是一个自定义操作帮助程序,其命名空间是自定义

如果你使用的是Ember.js,那么你可能应该使用Ember的数据持久性组件ember-data。

我建议您阅读 emberjs.com 指南。使用 Ember .js 不能掉以轻心,需要遵守惯例和标准。您需要重新考虑当前的方法。

ember-data 实现了一个客户端 RESTFUL API 接口。就像在服务器上一样,您应该在客户端上指定模型。Ember.js是一个框架,而不是一个库。用它!

您还可以像这样检查请求是否是 ajax(在控制器中):

$this->_request->isXmlHttpRequest()

有了这个,你可以调用任何操作,做需要做的事情,然后,根据你从请求检查中获得的值,你要么让视图呈现,要么在AJAX调用的情况下,打印出一个json字符串。

public function dosomethingAction(){
    // do something ...
    $this->view->data = $data = array(
        'param1' => 'v1'
        'param2' => 'v2'
    );
    if($this->_request->isXmlHttpRequest()){
        $this->getHelper('json')->sendJson($data);
    }
}