<?php
namespace App\Controller;
use App\Entity\Post;
use App\Form\PostType;
use App\Entity\User;
use App\Repository\PostRepository;
use Doctrine\ORM\Mapping\Id;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/post")
*/
class PostController extends AbstractController
{
/**
* @Route("/", name="post_index", methods={"GET"})
* @IsGranted("ROLE_USER", message="No access! Get out!")
*/
public function index(PostRepository $postRepository): Response
{
$post = $postRepository->findAll();
//dump($post);die();
return $this->render('post/index.html.twig', [
'posts' => $post,
]);
}
/**
* @Route("/new", name="post_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$post = new Post();
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
// $image=$form->getData()->getImageFile();
// dump($form,$image);die();
$post->setCreatAt(new \DateTime());
$post->setUpdateAt(new \DateTime());
$post->setDeleteAt(new \DateTime());
$post->setSlug($post->getTitle().'-'.uniqid());
$nom=$this->getUser();
$post->setAuteur($nom);
$entityManager->persist($post);
$entityManager->flush();
// $entityManager->flush();
$this->addFlash(
'success',
"Votre enregistrement a été effectué avec succès!"
);
return $this->redirectToRoute('post_index');
}
return $this->render('post/new.html.twig', [
'post' => $post,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="post_show", methods={"GET"})
*/
public function show(Post $post): Response
{
return $this->render('post/show.html.twig', [
'post' => $post,
]);
}
/**
* @Route("/{id}/edit", name="post_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Post $post): Response
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$post->setSlug($post->getTitle().'-'.uniqid());
$this->getDoctrine()->getManager()->flush();
$post->setAuteur($this->getUser());
$this->addFlash(
'success',
"Votre modification a été effectué avec succès!"
);
return $this->redirectToRoute('post_index');
}
return $this->render('post/edit.html.twig', [
'post' => $post,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="post_delete", methods={"DELETE"})
*/
public function delete(Request $request, Post $post): Response
{
if ($this->isCsrfTokenValid('delete'.$post->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($post);
$entityManager->flush();
}
return $this->redirectToRoute('post_index');
}
/**
* @Route("/article/{id}", name="post_show", methods={"GET"})
*/
public function postshow(String $id): Response
{
$repository = $this->getDoctrine()->getRepository(Post::class);
$post = $repository->findOneBy([
'id'=>$id
]);
$latest= $repository->findBylast();
return $this->render('front/article.html.twig', [
'post' => $post,
'latest' => $latest,
]);
}
/**
* @Route("/information/{id}", name="new_post_show", methods={"GET"})
*/
public function newpostshow(String $id): Response
{
$repository = $this->getDoctrine()->getRepository(Post::class);
$post = $repository->findOneBy([
'id'=>$id
]);
//dd($post);
$latest= $repository->findBylast();
//dd($latest);
return $this->render('newwebfront/article.html.twig', [
'post' => $post,
'latest' => $latest,
]);
}
}