Zend_Form在模型视图控制器的范式中适合什么位置 [英] Where does Zend_Form fit in the Model View Controller paradigma

查看:83
本文介绍了Zend_Form在模型视图控制器的范式中适合什么位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zend Framework 主要用于MVC. Zend_Form .

The Zend Framework is mainly meant for MVC use. One of the very usefull components is Zend_Form.

我很难找到Zend_Form的位置.它是视图,模型或控制器的一部分,我应该赋予它什么职责.

I have a bit trouble finding the place of Zend_Form. Is it part of the view, model, or controller and which responsibilities should I give it.

问题是,Zend_Form做两件事:装饰和渲染表单并验证它.第一个是实景任务,第二个是实模型任务.

The thing is, Zend_Form does two things: decorate and render the form and validate it. The first is a real view task, the second a real model task.

现在,最常见的用法似乎是使表单仅与控制器交互,从而有效地将两个任务(渲染和验证)都交给视图/控制器.

Now the most common use seems to be to have the forms interact with the controller only, effectively putting both tasks (rendering and validating) to the view/controller.

Matthew Weier O'Phinney 是将表单附加到模型上,并在控制器中添加以后的视图选项.

Another option given by Matthew Weier O'Phinney is to attach the form to your model, and adding the later view options in the controller.

所以,我对此表示怀疑.在MVC模式中应将Zend_Form放置在何处,以及如何使用它?

So, I'm doubting. Where in the MVC pattern should I place Zend_Form and how should I use it?

编辑到目前为止,很好的回答,谢谢!我将在赏金过期前的一两个小时内奖励它,因此,如果您有其他想法,请给出答案!

Edit Good answers so far, thanks! I will be awarding the bounty an hour or two before it expires, so please give an answer if you have some more thoughts!

推荐答案

Zend_Form可以在不同的位置查看.完全不能将其视为仅一层MVC模式的一部分.

Zend_Form can be viewed at different points. It can't be considered at all as part of just one layer of MVC pattern.

首先Zend_Form使用装饰器和视图助手来渲染表单,这时它是视图层的一部分. 然后,Zend_Form完成模型作业的一部分,以过滤和验证内容.

First of all Zend_Form use decorators and view helpers to render the form, at this point it is part of view layer. Then, Zend_Form does part of the model job filtering and validating the content.

我们知道Controller层从视图渲染输入并将其传递给模型.实际上,控制器层决定从模型层加载哪个资源,然后执行正确的调用.

We know that Controller layer render input from the view and pass it to the model. Actually, the controller layer decide which resource to load from model layer and then perform the corrects calls.

当您从控制器层调用Zend_Form时,您可以认为您正在调用一个模型资源来执行评估和过滤操作,并确定这是否为有效输入.例如:

When you call Zend_Form from controller layer, you can consider that you are calling one model resource to perform valitations and filtering actions and decide whether or not this is a valid input. For example:

public function newAction()
{
    $form = $this->getForm();

    if($this->getRequest()->isPost()) 
    {
        $formData = $this->_request->getPost();

        if($form->isValid($formData))
        {
            $Model = $this->getModel();
            $id = $Model->insert($form->getValues());
        }
    }

    $this->view->form = $form;
}

将表单与模型联系起来是一个很好的实践,因为当您执行过滤和验证操作时,您就位于模型层.因此,正如马修(Matthew)提议的那样:

Tie Forms to the model can be considered a good pratice because when you are performing filtering and validation actions you are on model layer. So, as Matthew proposed:

class Model_DbTable_Users extends Zend_Db_Table
{
    protected $_name = 'users';  
    protected $_form;

    public function getForm()
    {
        if(!$this->_form)
            $this->_form = new Form_User();
        return $this->_form;
    }

    public function add($data)
    {
        $form = $this->getForm();
        if(!$form->isValid($data)) return false;

        if($form->getValue('id'))
        {
            $id = (int) $form->getValue('id');
            $this->update($form->getValues(), 'id =' . $id);
        }   
        else
        {
            $id = $this->insert($form->getValues());
        }
        return $id;
    }
}

从标准目录结构中,我们可以看到Forms既不在模型文件夹中也不在视图文件夹中,因为Zend_Form是将许多资源和图层捆绑在一起的特定类.如果您查看Matthews的帖子,您将意识到,正是在视图脚本上设置了操作URL并将表单绑定到模型时,这正是在说的话.

From the standard directory structure we can see that Forms aren't in the model folder nor in the view folder because Zend_Form is a specific class that tie many resources and layers together. If you check the Matthews post you will realize that this is exactly what is being said when the action url is set on the view script and the form is tied to the model.

最后,您可以分析上下文并选择这两种方法之一.

Finally, you can analyze your context and pick up one of these two approachs.

当前,我的选择是将表单绑定到模型.看起来不错!对我来说很有道理.

Currently, my choice is to tie forms to models. Looks nice! And make a lot of sense to me.

这篇关于Zend_Form在模型视图控制器的范式中适合什么位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆