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

发布于 2024-10-09 00:00:29 字数 1444 浏览 11 评论 0原文

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

I have two actions that are essentially identical, but need different URLs. Normally I would use _forward() to render the other action:

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

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

However, I have some code the is happening in preDispatch() that I only want to execute once:

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';
    }
}

So I thought maybe I could simply call the function directly, like this:

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';
    }
}

But now Zend Framework is complaining about not being able to find the action-one.phtml view script. I don't want to render actionOne's view script. I want to render actionTwo's view script. What do I need to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2024-10-16 00:00:29

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

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

Using render() seems to do the trick:

public function actionOneAction()
{
    $this->actionTwoAction(); //execute the same code as actionTwoAction()
    $this->render('action-two'); //renders the same view as actionTwoAction()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文