Skip to content

Commit 027b9f5

Browse files
Add WithCustomRepositoryLegacy.php to include the renderForm() version
1 parent 1650332 commit 027b9f5

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

tests/Maker/MakeCrudTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\MakerBundle\Test\MakerTestCase;
1616
use Symfony\Bundle\MakerBundle\Test\MakerTestRunner;
1717
use Symfony\Component\Yaml\Yaml;
18+
use Symfony\Component\HttpKernel\Kernel;
1819

1920
class MakeCrudTest extends MakerTestCase
2021
{
@@ -177,7 +178,7 @@ public function getTestDetails(): \Generator
177178
$this->runCrudTest($runner, 'it_generates_basic_crud.php');
178179

179180
self::assertFileEquals(
180-
sprintf('%s/fixtures/%s', \dirname(__DIR__), 'make-crud/expected/WithCustomRepository.php'),
181+
sprintf('%s/fixtures/make-crud/expected/WithCustomRepository%s.php', \dirname(__DIR__), Kernel::VERSION_ID < 60200 ? 'Legacy' : ''),
181182
$runner->getPath('src/Controller/SweetFoodController.php')
182183
);
183184
}),
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use App\Entity\SweetFood;
6+
use App\Form\SweetFoodType;
7+
use App\Repository\SweetFoodRepository;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Response;
12+
use Symfony\Component\Routing\Annotation\Route;
13+
14+
#[Route('/sweet/food')]
15+
class SweetFoodController extends AbstractController
16+
{
17+
#[Route('/', name: 'app_sweet_food_index', methods: ['GET'])]
18+
public function index(SweetFoodRepository $sweetFoodRepository): Response
19+
{
20+
return $this->renderForm('sweet_food/index.html.twig', [
21+
'sweet_foods' => $sweetFoodRepository->findAll(),
22+
]);
23+
}
24+
25+
#[Route('/new', name: 'app_sweet_food_new', methods: ['GET', 'POST'])]
26+
public function new(Request $request, EntityManagerInterface $entityManager): Response
27+
{
28+
$sweetFood = new SweetFood();
29+
$form = $this->createForm(SweetFoodType::class, $sweetFood);
30+
$form->handleRequest($request);
31+
32+
if ($form->isSubmitted() && $form->isValid()) {
33+
$entityManager->persist($sweetFood);
34+
$entityManager->flush();
35+
36+
return $this->redirectToRoute('app_sweet_food_index', [], Response::HTTP_SEE_OTHER);
37+
}
38+
39+
return $this->renderForm('sweet_food/new.html.twig', [
40+
'sweet_food' => $sweetFood,
41+
'form' => $form,
42+
]);
43+
}
44+
45+
#[Route('/{id}', name: 'app_sweet_food_show', methods: ['GET'])]
46+
public function show(SweetFood $sweetFood): Response
47+
{
48+
return $this->renderForm('sweet_food/show.html.twig', [
49+
'sweet_food' => $sweetFood,
50+
]);
51+
}
52+
53+
#[Route('/{id}/edit', name: 'app_sweet_food_edit', methods: ['GET', 'POST'])]
54+
public function edit(Request $request, SweetFood $sweetFood, EntityManagerInterface $entityManager): Response
55+
{
56+
$form = $this->createForm(SweetFoodType::class, $sweetFood);
57+
$form->handleRequest($request);
58+
59+
if ($form->isSubmitted() && $form->isValid()) {
60+
$entityManager->flush();
61+
62+
return $this->redirectToRoute('app_sweet_food_index', [], Response::HTTP_SEE_OTHER);
63+
}
64+
65+
return $this->renderForm('sweet_food/edit.html.twig', [
66+
'sweet_food' => $sweetFood,
67+
'form' => $form,
68+
]);
69+
}
70+
71+
#[Route('/{id}', name: 'app_sweet_food_delete', methods: ['POST'])]
72+
public function delete(Request $request, SweetFood $sweetFood, EntityManagerInterface $entityManager): Response
73+
{
74+
if ($this->isCsrfTokenValid('delete'.$sweetFood->getId(), $request->request->get('_token'))) {
75+
$entityManager->remove($sweetFood);
76+
$entityManager->flush();
77+
}
78+
79+
return $this->redirectToRoute('app_sweet_food_index', [], Response::HTTP_SEE_OTHER);
80+
}
81+
}

0 commit comments

Comments
 (0)