2

我有两个基本相同的操作,但需要不同的 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 的视图脚本。我需要做什么?

4

1 回答 1

3

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

public function actionOneAction()
{
    $this->actionTwoAction(); //execute the same code as actionTwoAction()
    $this->render('action-two'); //renders the same view as actionTwoAction()
}
于 2010-12-21T21:19:10.557 回答