我正在使用具有模块化结构的Symfony2构建税务局自助服务应用程序。
这些模块包含在单独的包中,例如TaxBillsBundle,DirectDebitBundle和PersonalDataBundle。 这些包包含前端/后端功能,实体等。
我们的想法是让AppBundle将模块粘合在一起。 现在,因为这是我在Symfony2中的第一个应用程序,我不知道如何处理它。
这是应用程序的主屏幕:
每个图块代表一个模块。 撰写此屏幕的最佳方法是什么? AppBundle负责显示磁贴,但不显示其内容。 添加新模块时,我不想触摸AppBundle。
一位同事建议使用事件系统。 AppBundle可以“广播”对模块(订户)可以响应的瓦片内容的请求。 收集完所有图块内容后,AppBundle就可以构成页面。 这是一种合理的方法吗? 还有其他想法吗?
我建议在每个非AppBundle包中使用一个订阅者类来实现symfony的事件调度程序并向其添加订阅者。
http://symfony.com/doc/current/components/event_dispatcher.html
如果它们是静态磁贴,我只需为整个应用程序的views目录中的每个创建一个twig模板: app/Resources/views/modules/dashboard-tiles/module-x.html.twig
并将其包含在仪表板中app/Resources/views/pages/dashboard.html.twig
视图文件。 如果它们的内容相似,您可以重构使用单个twig模块文件并将其传递给数据:
// DashboardController.php
public function dashboardAction()
{
/** You could use an entity and create new objects for tiles instead of building an array like this. Or a method to return each one. **/
$tiles = [];
$tiles[] = [
'name' => 'Personal Data',
'links' => [
{
'text' => 'Show data',
'icon' => 'fa-user',
'href' => '#'
},
{
'text' => 'Edit data',
'icon' => 'fa-edit',
'href' => '#'
}
]
];
return $this->templating->renderResponse(
'PathTo/dashboard.html.twig', [ 'tiles' => $tiles ]);
}
// dashboard.html.twig
{% for tile in tiles %}
{% include 'modules/dashboard-tiles/tile.html.twig' with {
'tile': tile
} %}
{% endfor %}
# tile.html.twig
<div class="tile">
<h2>{{ name }}</h2>
{% for link in links %}
<a href="{{ href }}"><i class="fa {{ icon }}"></i> {{ text }}
{% endfor %}
</div>
如果它们是动态的,我会在每个包中设置一个服务,使用一种方法返回视图所需的数据,该数据可以从控制器调用并发送到twig。 您可能希望使用依赖注入将服务引入控制器,然后使用它们:
$tiles[] = $this->personalDataService->getDashboardTile();
但看起来瓷砖只包含按钮,所以为什么不保持简单。