【问题标题】:How to set two views in a controller in Zend frameworkZend框架如何在一个控制器中设置两个视图
【发布时间】:2014-02-06 02:20:15
【问题描述】:

我正在尝试在 Zend 1.12 中提交表单后创建感谢页面

在索引中我有表单,如果验证通过,我希望转到另一个视图(非索引)以获取感谢页面。我如何在我的代码中做到这一点:

public function indexAction()
{
    // action body
    $C_form = new Application_Form_Eform();
    if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($C_form->isValid($formData)) {
            $this->_helper->redirector('','result');
            exit;
            } else {
            $C_form->populate($formData);
            }
            }
    $this->view->form = C_eform;
}

然后我应该在哪里创建 .phtml 文件?在应用程序\视图\脚本\索引?

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    我想你在找render:

    $this->view->render('index/yourotherview.phtml');
    

    在这种情况下,index/ 指的是您的views/scripts/index 文件夹和yourotherview.phtml 文件。

    所以,总而言之就是:

    public function indexAction()
    {
        // action body
        $C_form = new Application_Form_Eform();
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($C_form->isValid($formData)) {
                $this->_redirect('/index/result);
            } else {
                $C_form->populate($formData);
            }
        }
        $this->view->form = C_eform;
    }
    

    编辑:

    从您的评论看来,您只是想被重定向而不是显示不同的视图。在这种情况下,就像创建一个新动作并为其创建视图一样简单:

    public function resultAction() {
        // Code here if you need it
    }
    

    然后在index/ 视图目录中创建文件result.phtml,您需要在索引控制器中使用$this->_redirect('/index/result');。 (见上面的代码)

    【讨论】:

    • 不工作,当表单正确时,它会显示An error occurred Application error 并且在 URL 中不会转到 index/result 或 /result ....
    • 看看我的编辑,我想这就是你要找的。​​span>
    • 酷,工作!谢谢!我可能对 zend 没有其他问题要解决 :) 如果有的话我会告诉你的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多