2

我的应用程序中的控制器很少,它们在控制器中使用了太多的业务逻辑。例如看看这个动作方法

public function publishedAction()
{
    if ($this->getRequest()->isXmlHttpRequest()) {
        $dataTable = new Model_DataTables('item i', array('i.id', 'i.title', 'i.from_age', 'i.to_age', 'i.gender', 'i.created_at', 'i.ispublished'));
        $item = new Model_Item();
        $output = $dataTable->aaData();
        $iResult = $dataTable->iResult();
        foreach($iResult as $k => $v) {
            $id = $iResult[$k][0];
            $iResult[$k][0] = date('d-m-Y', strtotime($iResult[$k][5]));
            $iResult[$k][2] = $item->formatAge($iResult[$k][2], $iResult[$k][3]);
            $iResult[$k][3] = ($iResult[$k][4] == 1) ? 'Male' : 'Female';
            $iResult[$k][4] = "<a href=/admin/item/view/".$id.">View</a>";
            $iResult[$k]['DT_RowId'] = "$id";
        }
        $output['aaData'] = $iResult;
        $this->_helper->json($output);
    }
}

参考上述动作方法,格式化数据的逻辑在控制器本身处理。我在同一个控制器中有几个这样的方法导致我的控制器变胖。而许多有经验的灵魂都同意让控制器尽可能瘦。我想问我应该将上述方法的所有业务逻辑移动到相应的模型中还是可以按照当前流程进行?

更新:

参考上面的代码,我将所有业务逻辑都放在了我的模型中

public function getPublishedItem()
{
    $dataTable = new Model_DataTables('item i', array('i.id', 'i.title', 'i.from_age', 'i.to_age', 'i.gender', 'i.created_at', 'i.ispublished'));
    $output = $dataTable->aaData();
    $iResult = $dataTable->iResult();
    foreach($iResult as $k => $v) {
        $id = $iResult[$k][0];
        $iResult[$k][0] = date('d-m-Y', strtotime($iResult[$k][5]));
        $iResult[$k][2] = $this->formatAge($iResult[$k][2], $iResult[$k][3]);
        $iResult[$k][3] = ($iResult[$k][4] == 1) ? 'Male' : 'Female';
        $iResult[$k][4] = "<a href=/admin/item/view/".$id.">View</a>";
        $iResult[$k]['DT_RowId'] = $id;
    }
    $output['aaData'] = $iResult;
    return $output;
}

并在控制器中

public function publishedAction()
{
    if ($this->getRequest()->isXmlHttpRequest()) {
        $item = new Model_Item();
        $this->_helper->json($item->getPublishedItem());
    }
}

这是正确的方法吗?还是我做错了?

4

1 回答 1

2

Since you retrieved $iResult from your model (Model_DataTables), wouldn't it be nicer to simply do:

public function publishedAction()
{
    if ($this->getRequest()->isXmlHttpRequest()) {
        $dataTable = new Model_DataTables('item i', array('i.id', 'i.title', 'i.from_age', 'i.to_age', 'i.gender', 'i.created_at', 'i.ispublished'));
        $output['aaData'] = $dataTable->getIResult();
        $this->_helper->json($output);
    }
}

where getIResult() contains your foreach.

Another solution would be to use a view helper, since it seems that you are formatting html content. Your action would then be like this:

public function publishedAction()
{
    if ($this->getRequest()->isXmlHttpRequest()) {
        $dataTable = new Model_DataTables('item i', array('i.id', 'i.title', 'i.from_age', 'i.to_age', 'i.gender', 'i.created_at', 'i.ispublished'));
        $output['aaData'] = $this->view->formatIResult($dataTable->getIResult());
        $this->_helper->json($output);
    }
}

where formatIResult() is the name of your view helper.

于 2012-04-19T15:29:38.130 回答