Zend_Form 在哪里适合模型视图控制器范例a [英] Where does Zend_Form fit in the Model View Controller paradigma

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

问题描述

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.

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

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

将表单绑定到模型可以被认为是一种很好的做法,因为当您执行过滤和验证操作时,您处于模型层.所以,正如马修所提议的:

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 在哪里适合模型视图控制器范例a的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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