【发布时间】:2016-02-12 13:20:05
【问题描述】:
我接手了一个 Symfony 2.6 项目,正忙于尝试尽可能多地重构胖控制器。
目前所有控制器的 jist 都遵循类似的结构
/**
* @Method({"GET"})
* @Route("/", name="homepage")
* @Template()
*/
public function indexAction()
{
//Initialize the API
$api = $this->get('core_bundle.api_wrapper')->getApi();
//Retrieve data from various end points
$result_set = $api->get('/rest/resource');
$result_set2 = $api->get('/rest/resource2');
$result_set3 = $api->get('/rest/resource3');
//etc.. etc..
//Once all api calls have been completed, do some manipulation of the data if required
$result_set = DataHelper::getInstance()->setReadFlags($result_set);
//The api resultset keys are not always standard (It's just the way it is Stackoverflow, I cannot change the API code)
$result_set2 = Standardize::getInstance()->standardizeKeys($result_set2);
$result_set2 = DataHelper::getInstance()->setReadFlags($result_set2);
//Create the social links from the existing data
$result_set3 = SocialHelper::getInstance()->createSocialLinks($result_set3);
return [$result_set, $result_set2, $result_set3];
}
我正在努力寻找一种适用于此类场景的良好模式。我需要清理控制器并从中移出尽可能多的业务逻辑,因为有些控制器有 100 多行代码。
我遇到的问题是:
- 每个控制器可以多次调用 API 以获取数据。数据并不总是相关的,有时调用取决于用户角色和/或之前 API 调用中的密钥。
- 每个结果集在返回到视图响应之前都需要以某种方式进行操作。
我觉得创建多个模型然后传入 API 结果集,然后调用一个函数来操作每个结果集将导致更小的控制器操作,但令人困惑,因为我将多次调用多个模型每个结果设置。
如果我想与单一职责保持内联,最终将不得不创建大量模型类。
任何指导将不胜感激。
【问题讨论】:
标签: php symfony design-patterns