2016-02-05 102 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 .. –

相關問題