I’m quite new to php, trying my hardest to make data from the Table pop up! Can someone check why this doesn’t work? Tried redownloading symfony, but still doesn’t seem like to work.
<?php
// src/Controller/AdminController.php
namespace AppController;
use AppEntityRecipe;
use AppFormRecipeType;
use DoctrineORMEntityManagerInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
class AdminController extends AbstractController
{
#[Route('/admin/recepten/nieuw', name: 'admin_recipe_new')]
public function newRecipe(Request $request, EntityManagerInterface $entityManager): Response
{
$recipe = new Recipe();
$form = $this->createForm(RecipeType::class, $recipe);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($recipe);
$entityManager->flush();
$this->addFlash('success', 'Recept toegevoegd!');
return $this->redirectToRoute('admin_recipes');
}
return $this->render('admin/new_recipe.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/admin/recepten/{id}/bewerken', name: 'admin_recipe_edit')]
public function editRecipe(Request $request, EntityManagerInterface $entityManager, Recipe $recipe): Response
{
$form = $this->createForm(RecipeType::class, $recipe);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
$this->addFlash('success', 'Recept bijgewerkt!');
return $this->redirectToRoute('admin_recipes');
}
return $this->render('admin/edit_recipe.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/admin/recepten/{id}/verwijderen', name: 'admin_recipe_delete')]
public function deleteRecipe(EntityManagerInterface $entityManager, Recipe $recipe): Response
{
$entityManager->remove($recipe);
$entityManager->flush();
$this->addFlash('success', 'Recept verwijderd!');
return $this->redirectToRoute('admin_recipes');
}
#[Route('/admin/recepten', name: 'admin_recipes')]
public function listRecipes(EntityManagerInterface $entityManager): Response
{
$recipes = $entityManager->getRepository(Recipe::class)->findAll();
return $this->render('admin/recipe_list.html.twig', [
'recipes' => $recipes,
]);
}
}
So that was the controller, apart from that I also have the twig:
{# templates/admin/recipe_list.html.twig #}
{% extends 'base.html.twig' %}
{% block title %}Recepten Overzicht{% endblock %}
{% block body %}
<h1>Recepten Overzicht</h1>
<table class="table">
<thead>
<tr>
<th>Naam</th>
<th>Omschrijving</th>
<th>Ingrediënten</th>
<th>Acties</th>
</tr>
</thead>
<tbody>
{% for recipe in recipes %}
<tr>
<td>{{ recipe.name }}</td>
<td>{{ recipe.description }}</td>
<td>
<ul>
{% for ingredient in recipe.ingredients %}
<li>{{ ingredient.name }}</li>
{% endfor %}
</ul>
</td>
<td>
<a href="{{ path('admin_recipe_edit', {id: recipe.id}) }}" class="btn btn-primary">Bewerken</a>
<a href="{{ path('admin_recipe_delete', {id: recipe.id}) }}" class="btn btn-danger">Verwijderen</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
Here I try adding the new recipe:
{# templates/admin/new_recipe.html.twig #}
{% extends 'base.html.twig' %}
{% block title %}Nieuw Recept{% endblock %}
{% block body %}
<h1>Nieuw Recept</h1>
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.description) }}
{{ form_row(form.ingredients) }}
<button class="btn">{{ button_label|default('Opslaan') }}</button>
{{ form_end(form) }}
{% endblock %}
Here I try editting the new recipes:
{# templates/admin/edit_recipe.html.twig #}
{% extends 'base.html.twig' %}
{% block title %}Recept Bewerken{% endblock %}
{% block body %}
<h1>Recept Bewerken</h1>
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.description) }}
<h2>Ingrediënten</h2>
<ul>
{% for ingredient in form.ingredients %}
<li>{{ form_row(ingredient) }}</li>
{% endfor %}
</ul>
<button type="submit" class="btn btn-primary">Opslaan</button>
{{ form_end(form) }}
{% endblock %}
Jimmy Oteiga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.