51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Vehicle;
|
|
use App\Form\VehicleType;
|
|
use App\Repository\VehicleRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
class VehicleController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly VehicleRepository $vehicleRepository
|
|
)
|
|
{
|
|
}
|
|
#[\Symfony\Component\Routing\Annotation\Route('/vehicle/add', name: 'vehicle_add')]
|
|
public function add(Request $request): Response
|
|
{
|
|
$vehicle = new Vehicle();
|
|
$form = $this->createForm(VehicleType::class, $vehicle);
|
|
$form->handleRequest($request);
|
|
|
|
if($form->isSubmitted() && $form->isValid())
|
|
{
|
|
$this->entityManager->persist($vehicle);
|
|
$this->entityManager->flush();
|
|
|
|
return $this->redirectToRoute("vehicle_list");
|
|
}
|
|
|
|
return $this->render('vehicle/add.html.twig', [
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
|
|
#[Route('/vehicle/list', name: 'vehicle_list')]
|
|
public function list(): Response
|
|
{
|
|
$vehicles = $this->vehicleRepository->findAll();
|
|
|
|
return $this->render('vehicle/list.html.twig', [
|
|
'vehicles' => $vehicles,
|
|
]);
|
|
}
|
|
} |