src/Controller/ProduitController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use App\Entity\Produit;
  8. use App\Form\ProduitType;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. class ProduitController extends AbstractController
  11. {
  12. /**
  13. * @Route("/produit", name="app_produit")
  14. */
  15. public function index(): Response
  16. {
  17. return $this->render('produit/index.html.twig', [
  18. 'produit' => 2,
  19. ]);
  20. }
  21. /**
  22. * @Route("/admin/produit", name="produit_list")
  23. */
  24. public function list(ManagerRegistry $doctrine): Response
  25. {
  26. $produits = new Produit();
  27. $em = $doctrine->getManager();
  28. $produits = $em->getRepository(Produit::class)->findAll();
  29. return $this->render('produit/liste.html.twig', [
  30. 'produits' => $produits,
  31. ]);
  32. }
  33. /**
  34. * @Route("/produit/2", name="produit_riseschool")
  35. */
  36. public function riseschool(): Response
  37. {
  38. return $this->render('produit/riseschool.html.twig', [
  39. 'produit' => 2,
  40. ]);
  41. }
  42. /**
  43. * @Route("/produit/3", name="produit_oula")
  44. */
  45. public function oula(): Response
  46. {
  47. return $this->render('produit/oula.html.twig', [
  48. 'produit' => 2,
  49. ]);
  50. }
  51. /**
  52. * @Route("/admin/produit/add", name="produit_add")
  53. */
  54. public function Add(Request $request, ManagerRegistry $doctrine)
  55. {
  56. $produit = new Produit();
  57. $em = $doctrine->getManager();
  58. $form = $this->createForm(ProduitType::class, $produit);
  59. if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  60. /** @var UploadedFile $photo */
  61. $photo = $form->get('illustration')->getData();
  62. if ($photo) {
  63. $year = date("Y");
  64. $month = date("m");
  65. $day = date("d");
  66. $hour = date("H");
  67. $minute = date("i");
  68. $seconde = date("s");
  69. $aleatoire = rand(0, 100);
  70. $photoName = "Photo" . $produit->getNom() . $day . $hour . $minute. $seconde. $aleatoire . "." . $photo->guessExtension();
  71. $photoName = str_replace(' ','',$photoName);
  72. try {
  73. $photo->move(
  74. $this->getParameter('produit_illustration_directory'), $photoName);
  75. } catch (FileException $e) {
  76. }
  77. $produit->setIllustration($photoName);
  78. }
  79. $em->persist($produit);
  80. $em->flush();
  81. return $this->redirectToRoute('produit_list');
  82. }
  83. return $this->render('produit/add.html.twig', [
  84. 'form' => $form->createView()]);
  85. }
  86. }