You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony框架中如何将支出添加表单整合至Dashboard页面的技术问询

Hey there! Let's figure out how to integrate your add expenditure form into the Dashboard page properly—no code duplication required. Here are two solid, Symfony-best-practice approaches to fix this:

This is Symfony's go-to method for reusing controller logic across pages, and it sounds like you might have missed a small step earlier. Let's walk through it correctly:

Step 1: Add a Route to Your addExpenditure Method

First, make sure your addExpenditure method has a proper route annotation (it was missing in your code snippet—this is probably why embedding didn't work):

#[Route('/addexpenditure/', name: 'add_expenditure')]
public function addExpenditure(ManagerRegistry $doctrine, Request $request) {
    $em = $doctrine->getManager();
    $expenditure = new Expenditure();
    $expenditure->setUser($this->getUser()); // Don't forget to link the current user!
    
    // Fix the variable name typo (you had $Expenditure with a capital E)
    $form = $this->createForm(ExpenditureType::class, $expenditure);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) { // Always validate the form!
        $em->persist($expenditure);
        $em->flush();

        // Redirect back to Dashboard after successful submission (prevents duplicate entries)
        return $this->redirectToRoute('dashboard');
    }

    // Match the variable name to what your template expects (you used `eintragenForm` in twig)
    return $this->render('expenditure/addexpenditure.html.twig', [
        'eintragenForm' => $form->createView()
    ]);
}

Step 2: Embed the Controller in Your Dashboard Template

In your dashboard.html.twig, add this wherever you want the form to appear (e.g., above the expenditure table):

<div class="add-expenditure-container">
    {{ render(controller('App\\Controller\\ExpenditureController::addExpenditure')) }}
</div>

That's it! This keeps your form logic isolated in its own controller, so you can reuse it elsewhere if needed.

2. Create the Form Directly in the Dashboard Controller

If you prefer to keep everything in one place (for simpler logic), you can instantiate the form directly in your dashboard method without duplicating form type code:

Update the dashboard Method

#[Route('/dashboard/', name: 'dashboard')]
public function dashboard(ExpenditureRepository $ar, ManagerRegistry $doctrine, Request $request) {
    $user = $this->getUser();
    $expenditures = $ar->findexpendituresOfUser($user);

    // Initialize the expenditure form
    $newExpenditure = new Expenditure();
    $newExpenditure->setUser($user);
    $addForm = $this->createForm(ExpenditureType::class, $newExpenditure);
    $addForm->handleRequest($request);

    // Handle form submission
    if ($addForm->isSubmitted() && $addForm->isValid()) {
        $em = $doctrine->getManager();
        $em->persist($newExpenditure);
        $em->flush();

        // Redirect to refresh the page
        return $this->redirectToRoute('dashboard');
    }

    return $this->render('expenditure/dashboard.html.twig', [
        'expenditures' => $expenditures,
        'addExpenditureForm' => $addForm->createView()
    ]);
}

Render the Form in Dashboard Template

Add this to your dashboard.html.twig where you want the form:

<div class="container">
    {{ form(addExpenditureForm) }}
</div>

This approach works well for simpler use cases where you don't need to reuse the form logic elsewhere.

A quick note: Don't forget to add validation to your Expenditure entity (like required fields for amount, description, etc.) to make sure the form submits cleanly!


内容的提问来源于stack exchange,提问作者LittleEast

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:22:39