简体   繁体   中英

How to call controller function in view in Zend Framework?

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 ?

First:

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

In your view script:

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

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

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

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

  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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM