比较“厚"Zend_Form 控制器正常吗?(如果没有,如何将它们稀释?) [英] Are relatively "thick" controllers normal with Zend_Form? (And if not, how does one thin them out?)

查看:22
本文介绍了比较“厚"Zend_Form 控制器正常吗?(如果没有,如何将它们稀释?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的表单:

I've got a form that looks like this:

class Cas_Form_Company extends Zend_Form
{
    /**
     * @param Cas_Model_Company|null $company
     */
    public function __construct(Cas_Model_Company $company = null)
    {
        parent::__construct();

        $id = new Zend_Form_Element_Hidden('id');

        $name = new Zend_Form_Element_Text('name');
        $name->addValidator('stringLength', false, array(2,45));
        $name->addValidator(new Cas_Model_Validate_CompanyUnique());
        $name->setLabel('Name');

        $submit = new Zend_Form_Element_Submit('Submit');

        if ($company)
        {
            $id->setValue($company->GetId());
            $name->setValue($company->GetName());
        }

        $this->addElements(array($id, $name, $submit));
        $this->setMethod('post');
        $this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . '/Asset/company');
    }

    public function Commit()
    {
        if (!$this->valid())
        {
            throw new Exception('Company form is not valid.');
        }

        $data = $this->getValues();
        if (empty($data['id']))
        {
            Cas_Model_Gateway_Company::Create($data['name']);
        }
        else
        {
            $company = Cas_Model_Gateway_Company::FindById((int)$data['id']);
            $company->SetName($data['name']);
            Cas_Model_Gateway_Company::Commit($company);
        }
    }
}

现在,这个表单依赖于一个控制器,看起来像这样:

Now, this form depends on a controller, which looks something like this:

public function companyAction()
{
    if ($this->getRequest()->isPost())
    {
        if ($this->getRequest()->getParam('submit') == 'Delete')
        {
            Cas_Model_Gateway_Company::Delete(Cas_Model_Gateway_Company::FindById((int)$this->getRequest()->getParam('id')));
            $this->_helper->redirector->setCode(303)->gotoSimple('companies');
        }

        $form = new Cas_Form_Company();
        if ($form->isValid($this->getRequest()->getParams()))
        {
            $form->Commit();
            $this->_helper->redirector->setCode(303)->gotoSimple('index');
        }
        $this->view->form = $form;
    }
    else if ($id = $this->getRequest()->getParam('id'))
    {
        $form = new Cas_Form_Company(Cas_Model_Gateway_Company::FindById($id));
        $this->view->form = $form;
    }
    else
    {
        $this->view->form = new Cas_Form_Company();
    }
    $this->_helper->viewRenderer->setScriptAction('formrender');
}

似乎控制器动作在这里做了太多",但我没有找到解决这个问题的简单方法.一般来说,我认为应该担心表单是添加或编辑或删除操作.但我似乎找不到一个好的方法来做到这一点.

It seems like the controller action is doing "too much" here, but I don't see an easy way to work around this. Generally speaking, I think the form should be the one worried about whether it's an add or edit or delete operation. But I can't seem to find a good way of going about doing that.

对于使用 Zend_Form 的人来说,这是正常模式还是我做错了什么?

Is this a normal pattern for someone using Zend_Form or have I done something wrong?

推荐答案

这完全没问题,您所做的就是将值发送到表单并处理响应、稍微重定向并处理视图.

That's perfectly fine, all you are doing is sending values to your form and handling the response, redirecting a bit and handling the view.

这些东西中的每一个都非常适合,如果您将它们放在其他地方,则很难找到.我不会认为胖控制器"是比其他控制器/操作需要更多 LOC",而是包含业务逻辑".如果您觉得圈复杂度变得太大,请尝试将您的操作拆分为较小的操作.

Each of these things fits really good and would be a hassle to find if you've placed them elsewhere. I wouldn't think of "fat controllers" as in "requires more LOC than other controllers/actions" but rather "contains business logic". If you feel the cyclomatic complexity is becoming too large, try splitting your action into smaller ones.

实际上,我会将 $form->Commit(); 部分重构为类似 $repository->create($form->getValues() 因为 a) Cas_Form_Company::Commit() 中没有使用任何内部内容,并且;b) 您希望将与存储相关的功能与验证和验证分开;显示您的表单.考虑调试/更改数据的存储方式,现在您将在哪里查看所有 f.ex.包含查询的类正在这样做,而且仅此而已 - 处理 DAL.

edit: Actually, I would refactor the $form->Commit(); part to something like $repository->create($form->getValues() since a) nothing internal is used in Cas_Form_Company::Commit(), and; b) you will want to have storage related functions separated from validating & displaying your form. Think about debugging/changing the way your data is stored and you will now where to look if all f.ex. classes containing queries are doing that and only that - handling the DAL.

这篇关于比较“厚"Zend_Form 控制器正常吗?(如果没有,如何将它们稀释?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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