1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
<?php
namespace App\Controller;
use App\Entity\Car;
use App\Form\CarType;
use App\Repository\CarRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/car")
*/
class CarController extends AbstractController
{
/**
* @Route("/", name="car_index", methods={"GET"})
*/
public function index(CarRepository $carRepository): Response
{
return $this->render('car/index.html.twig', [
'cars' => $carRepository->findAll(),
]);
}
/**
* @Route("/new", name="car_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$car = new Car();
$form = $this->createForm(CarType::class, $car);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($car);
$entityManager->flush();
return $this->redirectToRoute('car_index');
}
return $this->render('car/new.html.twig', [
'car' => $car,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="car_show", methods={"GET"})
*/
public function show(Car $car): Response
{
return $this->render('car/show.html.twig', [
'car' => $car,
]);
}
/**
* @Route("/{id}/edit", name="car_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Car $car): Response
{
$form = $this->createForm(CarType::class, $car);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('car_index');
}
return $this->render('car/edit.html.twig', [
'car' => $car,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="car_delete", methods={"DELETE"})
*/
public function delete(Request $request, Car $car): Response
{
if ($this->isCsrfTokenValid('delete'.$car->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($car);
$entityManager->flush();
}
return $this->redirectToRoute('car_index');
}
} |