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
| #[Route('/video/new', name:'video.new', methods: ['get', 'post'])]
public function new(Request $request,EntityManagerInterface $manager): Response{
//create a new video
$video = new Video();
//bind form with videoType reference
$form = $this->createForm(VideoType::class,$video);
//send request to the database
$form->handleRequest($request);
//if is submitt is clicked and all valid in form
if($form->isSubmitted() && $form->isValid()) {
$video = $form->getData();
//manager send new video "object" in the database
$manager->persist($video);
$manager->flush();
$this->addFlash("success","Video added successfully");
return $this->redirectToRoute("video.index");
}
return $this->render('video/new.html.twig',[
'form'=>$form->createView(),
]);
}
#[Route('/video/edit/{id}', name: 'video.edit', methods: ['get', 'post'])]
public function edit(VideoRepository $repoVideo,Int $id,EntityManagerInterface $manager,Request $request): Response{
$video = $repoVideo->findOneBy(["id" => $id ]);
$form = $this->createForm(VideoType::class,$video);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$video = $form->getData();
//manager send new video "object" in the database
$manager->persist($video);
$manager->flush();
$this->addFlash("success","Video updated successfully");
return $this->redirectToRoute("video.index");
}
return $this->render('video/edit.html.twig',[
'form'=>$form->createView(),
]);
} |
Partager