【问题标题】:Zend Framework: Chaining forms: how can I "unset $this->getRequest->isPost()"?Zend 框架:链接表单:如何“取消设置 $this->getRequest->isPost()”?
【发布时间】:2010-08-31 14:21:36
【问题描述】:

这是一个非常基本的事情,但我不知道如何使用 Zend 框架“正确”解决这个问题:

场景:

  1. 页面显示表格1,
  2. 页面展示形式2

这是一个非常基本的事情,但我不知道如何使用 Zend 框架“正确”解决这个问题:

场景:

  1. 页面显示表格1,
  2. 页面显示表格2
类 FooController 扩展 Zend_Controller_Action { ... 公共函数 form1Action(){ if ($this->getRequest()->isPost()) { //将form1中的数据保存到数据库中 $this->_forward('form2'); } //显示表格1 } 公共函数 form2Action(){ if ($this->getRequest()->isPost()) { //将form2中的数据保存到数据库中 $this->_forward('somewhere'); } //显示form2 } }

当用户发布form1时,首先执行form1Action中的if-condition(这就是我想要的),还有form2Action中的if-condition。

“取消设置 $this->getRequest()->isPost()”的正确方法是什么?

注意:表单是“手工”构建的(不使用 Zend Form)

【问题讨论】:

  • 你不应该在某个时候使用isValid() 吗?这应该会阻止您在发布 form1 时处理 form2 中的数据,除非它们当然完全相同。或者使用 Zend 的内置多页表单记录在这里 framework.zend.com/manual/en/zend.form.advanced.html
  • 非常有意义,但在这种情况下,表单是“手工”构建的,即不使用 Zend_Form...

标签: php zend-framework


【解决方案1】:

您有三个选择:

  1. 使用_redirect 而不是_forward。在同一请求下转发重定向。重定向将创建一个新请求。'
  2. 在你的_forward调用中设置一个参数,你可以在你的第二个表单中检查它:例如'form' => 2.More information.
  3. 立即使用 Zend_Form 中包含的built in multipage forms

【讨论】:

  • 我暂时就是这么解决的,但是我对这个解决方案并不满意,因为创建一个新的请求对服务器来说意味着更大的压力。我想避免这种情况.. :-)
  • 我已经更新了我的答案以包含一个新选项。如果您仍想使用 _forward,选项 #2 将为您提供解决方案。
【解决方案2】:

您总是可以在动作一中设置一个类变量,如果它是真的,不要在动作二中运行代码。

类似:

class FooController extends Zend_Controller_Action {  
private $_fromAction1 = false;
    ...  
    public function form1Action(){  
        if ($this->getRequest()->isPost()) {  
           // save data from form1 in database  
$this->_fromAction1 = true;
           $this->_forward('form2');  
        }  
        // display form1  
    }  
    public function form2Action(){  
        if ($this->getRequest()->isPost() && !$this->_formAction1) {  
           // save data from form2 in database  
           $this->_forward('somewherelese');  
        }  
        // display form2  
    }  
} 

【讨论】:

    【解决方案3】:

    最后一个选项对我不起作用。

    $this->_forward() 创建控制器的新实例,因此在第一个实例中设置变量不会影响新实例中的变量。

    我的解决方案是将 $_fromAction1 设为静态以在 2 个实例之间共享变量。

    class FooController extends Zend_Controller_Action {
    private static $_fromAction1 = false;
        ...  
        public function form1Action(){  
            if ($this->getRequest()->isPost()) {  
               // save data from form1 in database  
    FooController::_fromAction1 = true;
               $this->_forward('form2');  
            }  
            // display form1  
        }  
        public function form2Action(){  
            if ($this->getRequest()->isPost() && !FooController::_formAction1) {  
               // save data from form2 in database  
               $this->_forward('somewherelese');  
            }  
            // display form2  
        }  
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      相关资源
      最近更新 更多