2012-10-19 42 views
1

在Zend Framework中,我有一個控制器如何在Zend Framework中調用控制器函數?

class TestController extends Zend_Controller_Action 
{ 

    public function indexAction() 
    { 

    } 

    public function getResultByID($id) 
    { 
     return $id; 
    } 

} 

我如何可以調用index.phtml功能getResultByID?

+1

你真的需要在視圖中調用'getResultByID()'嗎?爲什麼不在控制器中調用該方法並將結果傳遞給視圖(這將是更標準的方法)? –

回答

0

如果執行indexAction你可以從那裏把它叫做:

public function indexAction() 
{ 
    $this->getResultByID((int) $_REQUEST['id']); 
} 
+0

因此無法直接在視圖中調用該函數? – user1745968

0

試試這個代碼,我覺得這是最好的選擇

public function indexAction() 
    { 
     $this->view->assign('id' => $this->getResultByID($this->_request->getParam('id', null))) 
    } 

    public function getResultByID($id = null) 
    { 
     return $id; 
    } 

,並考慮:回聲$this->id

0
public function getResultByID($id) 
    { 
       return $id; 
    } 

而不是上面的代碼你可以使用

public function getResultByID($id) 
    { 
     this->view->id=$id; 
     this->render('index.phtml'); 
    } 

那麼你可以使用id的值在index.phtl爲this->id

5

第一:

public function indexAction() 
{ 
    $this->view->controller = $this 
} 

在視圖腳本:

<html><title><?php echo $this->controller->getResultByID($this->id); ?></title></html> 
+0

這是我想說的最好答案。爲什麼人們不接受! –

0

在您的indexAction方法中,返回數組中的getResultByID方法。

控制器:

public function indexAction() 
{ 
    return array(
     'getResult' => $this->getResultByID($id), 
    ); 
} 

public function getResultByID($id) 
{ 
    return $id; 
} 

的問題是,你在那裏會得到$ ID。無論如何,在像這樣的變量中調用getResult字符串。

查看:

echo $getResult; 

就是這樣。

相關問題