Zend Framework:如何呈现不同的动作?

时间:2010-12-21 20:43:49

标签: zend-framework zend-view zend-controller

我有两个基本相同的操作,但需要不同的URL。通常我会使用_forward()来渲染其他动作:

class MyController extends Zend_Controller_Action
{
    public function actionOneAction()
    {
        $this->_forward('action-two');
    }

    public function actionTwoAction()
    {
        $this->view->foobar = 'foobar';
    }
}

但是,我在preDispatch()中发生了一些我只想执行一次的代码:

class MyController extends Zend_Controller_Action
{
    public function preDispatch()
    {
        //execute this only once before actionOne or actionTwo, but not both
    }

    public function actionOneAction()
    {
        $this->_forward('action-two'); //this won't work because preDispatch() will get called a second time
    }

    public function actionTwoAction()
    {
        $this->view->foobar = 'foobar';
    }
}

所以我想也许我可以直接调用这个函数,就像这样:

class MyController extends Zend_Controller_Action
{
    public function preDispatch()
    {
        //execute this only once before actionOne or actionTwo, but not both
    }

    public function actionOneAction()
    {
        $this->actionTwoAction(); //execute the same code as actionTwoAction()
    }

    public function actionTwoAction()
    {
        $this->view->foobar = 'foobar';
    }
}

但是现在Zend Framework抱怨无法找到action-one.phtml视图脚本。我不想渲染actionOne的视图脚本。我想渲染actionTwo的视图脚本。我需要做什么?

1 个答案:

答案 0 :(得分:3)

使用render()似乎可以解决问题:

public function actionOneAction()
{
    $this->actionTwoAction(); //execute the same code as actionTwoAction()
    $this->render('action-two'); //renders the same view as actionTwoAction()
}