2016-02-05 93 views
2

我在酒店模块中有一个控制器SearchController,我有一个方法在这个控制器中搜索如下。如何在zend中调用另一个控制器的方法?

public function searchAction() 
{ 

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

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

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

现在我想调用getlatlng方法searchAction方法。它返回当前位置的经度和纬度。我将纬度和纬度传递给使用post或get的getlatlng函数。

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

+0

您可以参照下面的链接:http://计算器.com/questions/886291/calling-member-function-of-other-controller-in-zend-framework – Dev

+0

Thanks @Dev,如何从任何视图调用getlatlng函数? –

+0

嗯,我不熟悉zend,因为我在ASP.NET MVC和PHP Laravel上工作。你仍然可以在这里得到一些帮助:http://stackoverflow.com/questions/12971421/how-to-call-controller-function-in-view-in-zend-framework?answertab=votes#tab-top – Dev

回答

1

你可以这样调用,如果使用的是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; 
    } 
} 

输出:

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

Thanks..it works .. –