如何在 zend 中调用另一个控制器的方法?

How to call a method of another controller in zend?

我在酒店模块中只有一个控制器 SearchController,我在该控制器中有一个搜索方法,如下所示。

public function searchAction()
{

  // Code for search
  // want to call getlatlng method
}

现在我在同一模块中创建了一个新控制器 DistanceController 并且 在距离 controller.my getlatlng 方法中创建了一个 getlatlng 方法,就像这样。

 public function getlatlng()
  {
         $location = $_REQUEST['lat'].','.$_REQUEST['lng'];
         return $location;
  }

现在我想在 searchAction 方法中调用一个 getlatlng 方法。其中 returns 当前 place.I 的纬度和经度 使用 post 或 get.

将纬度和经度传递给 getlatlng 函数

那么如何在 searchAction 方法中调用 getlatlng 方法呢?

你可以这样调用,如果你使用的是ZF版本1.x:

class SearchController extends Zend_Controller_Action 
{
    public function searchAction() {
        echo "search_action_from_SearchController";
        require_once ('DistanceController.php');
        $distanceCtrl = new DistanceController($this->_request, $this->_response);
        $distanceCtrl->xyzAction();
        die;
    }
}

class DistanceController extends Zend_Controller_Action 
{
    public function getlatlng() {
        echo "getlatlng_from_DistanceController";
        die;
    }
}

Output:

URL: http://www.example.com/search/search
    search_action_from_SearchController
    getlatlng_from_DistanceController