简体   繁体   中英

Different Variables/Functions for one twig.html

Since 3 weeks i try to learn php with the symfony framework. I want to build an application with which i can track my expanses.

I made good progress but since 2 days i have a little logic problem so maybe someone can help me here.

I want to make a dashboard.(the main side of the project) There the user can monitor the expenditures. This works. Now i want also a form at the dashboard, so the user can add new expenditures. I already implement a form but with a extra route. So in my ExpenditureController i have the functions dashboard and the function addExpenditure which generate different twig.html templates.

So the user can monitor his expenditures with...budgetapp/expenditure/dashboard and he can add new Expenditure with...budgetapp/expenditure/addexpenditure

My Dashboard-Function

    #[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,
       ]);
}

The expenditure/dashboard.html.twig shows the Expentiures of the current user in a table

My addExpenditure-Function public 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()
    ]);
}

The expenditure/addexpenditure.html.twig looks like this:

{% block body %}
<div class="container">
{{form(eintragenForm)}}
</div>
{% endblock %}

My problem /mistake in thinking: How can i implement the form to the dashboard? So of course i can take the code from the addexpenditure function and put it 1:1 in the dashboard-funciton. but i dont think this is the right way? I also tried to including template fragments with the offical Embedding Controllers Documentation of Symfony , but this also dont work. So someone can help me with a suggestion how you would handle this in your project?

Best regards Markus

First of all, do not use embedded controllers. This feature is designed for pretty rare cases, like symfony debug toolbar, etc.

There are two possible solutions: put both view and add logic inside one controller's action, or separate them. Since you probably have some validation, it's reasonable to have both add and view code inside one action (otherwise, you'll have to pass errors via sessions, which is not very pretty). So basically, your dashboard/add action will look like this:

#[Route('/dashboard/', name: 'dashboard')]
public function dashboard(Request $request, ExpenditureRepository $ar, ManagerRegistry $doctrine)
{
    $expenditure = new Expenditure();
    $form = $this->createForm(ExpenditureType::class, $expenditure);

    if ($request->isMethod('POST')) {
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $doctrine->getManager();
            $em->persist($expenditure);
            $em->flush();

            return $this->redirectToRoute('dashboard');
        }
    }

    $user = $this->getUser();
    $expenditures = $ar-> findexpendituresOfUser($user);

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

What's happening here:

  • First you check if POST http method was used, if it was - it means that form has been submitted and you can handle it.
  • If this is a GET request, just show ordinary dashboard
  • If the form is submitted and it's valid - save data and redirect to the same page. Otherwise, you'll need to empty all submitted data from the form, etc. (it's not the right way to do it)
  • If the form is invalid, do not redirect, just render the page normally, and all errors will be shown.

Finally, you of course have to render the form on your dashboard.html.twig like you did: {{form(eintragenForm)}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM