From c806227de251f45633b2f4dd936350c6111d8951 Mon Sep 17 00:00:00 2001 From: Hugo PRADIER Date: Fri, 7 Jun 2024 15:24:29 +0200 Subject: [PATCH] ajout edit post --- src/Controller/PostController.php | 18 ++++++++++++++++++ templates/post/edit.html.twig | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 templates/post/edit.html.twig diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index b0bc747..a6320a9 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -47,4 +47,22 @@ class PostController extends AbstractController 'form' => $form, //->createView(), ]); } + + #[Route('/posts/edit/{id}', name: 'app_edit_post')] + #[IsGranted('ROLE_USER', message: 'You must be logged in to access this page.')] + public function edit(Request $request, EntityManagerInterface $entityManager, Post $post): Response +{ + $form = $this->createForm(PostType::class, $post); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('app_posts'); + } + + return $this->render('post/edit.html.twig', [ + 'form' => $form->createView(), + ]); +} } diff --git a/templates/post/edit.html.twig b/templates/post/edit.html.twig new file mode 100644 index 0000000..1c8e0bb --- /dev/null +++ b/templates/post/edit.html.twig @@ -0,0 +1,15 @@ +{% extends 'base.html.twig' %} + +{% block title %}Edit Post{% endblock %} + +{% block body %} +

Edit Post

+ + {{ form_start(form) }} + {{ form_widget(form) }} + + + {{ form_end(form) }} + + Back to posts +{% endblock %} \ No newline at end of file