【发布时间】:2022-01-09 10:43:01
【问题描述】:
自从 3 周以来,我尝试使用 symfony 框架学习 php。 我想构建一个可以跟踪我的扩展的应用程序。
我取得了不错的进展,但由于 2 天以来我有一点逻辑问题,所以也许有人可以在这里帮助我。
我想做一个仪表板。(项目的主要方面)用户可以在那里监控支出。 这行得通。现在我还想在仪表板上有一个表格,这样用户就可以添加新的支出。我已经实现了一个表单,但有一条额外的路线。所以在我的 ExpenditureController 我有 functions dashboard 和 function addExpenditure 生成不同的 twig.html 模板。
因此用户可以使用 ...budgetapp/expenditure/dashboard 监控他的支出 他可以用 ...budgetapp/expenditure/addexpenditure 添加新的 Expenditure
我的仪表板功能
#[Route('/dashboard/', name: 'dashboard')]
public function dashboard(ExpenditureRepository $ar)
{
$user = $this->getUser();
$expenditures = $ar-> findexpendituresOfUser($user);
return $this->render('expenditure/dashboard.html.twig', [
'expenditures' => $expenditures,
]);
}
支出/dashboard.html.twig 在表格中显示当前用户的支出
我的 addExpenditure-Function 公共函数 addExpenditure (ManagerRegistry $doctrine, Request $request){
$em = $doctrine->getManager();
$expenditure = new Expenditure();
$form = $this->createForm(ExpenditureType::class, $Expenditure);
$form->handleRequest($request);
if($form->isSubmitted()){
$em->persist($expenditure);
$em->flush();
}
return $this->render('expenditure/addexpenditure.html.twig', [
'addexpenditureForm' => $form->createView()
]);
}
支出/addexpenditure.html.twig 如下所示:
{% block body %}
<div class="container">
{{form(eintragenForm)}}
</div>
{% endblock %}
我的问题/思维错误: 如何将表单实现到仪表板?因此,我当然可以从 addexpenditure 函数中获取代码,并将其 1:1 放入仪表板功能中。但我不认为这是正确的方法?我还尝试在官方Embedding Controllers Documentation of Symfony 中包含模板片段,但这也不起作用。 所以有人可以帮助我建议您在项目中如何处理这个问题?
最好的问候 马库斯
【问题讨论】:
标签: php forms symfony twig dashboard