How to call controller function in view in Zend Framework?

你离开我真会死。 提交于 2019-12-31 04:32:06

问题


In Zend Framework, I have one controller

class TestController extends Zend_Controller_Action
{

    public function indexAction()
    {

    }

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

}

How can I call the function getResultByID in index.phtml ?


回答1:


First:

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

In your view script:

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



回答2:


By using Action View Helper :

http://framework.zend.com/manual/1.12/en/zend.view.helpers.html#zend.view.helpers.initial.action




回答3:


If the indexAction is executed you could call it from there:

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



回答4:


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

instead of the above code u can use

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

then you can use the value of id in index.phtl as this->id




回答5:


Try this code I think this is best choice

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

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

And in view: echo $this->id




回答6:


In your indexAction method, return the getResultByID method in an array.

Controller:

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

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

The question is where will you get the $id. Anyway, call the getResult string in a variable like this.

View:

echo $getResult;

And that's it.



来源:https://stackoverflow.com/questions/12971421/how-to-call-controller-function-in-view-in-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!