src/Controller/HomeController.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\UserType;
  5. use App\Repository\UserRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class HomeController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/", name="homepage")
  17.      */
  18.     public function index(): Response
  19.     {
  20.         return $this->render('home/homepage.html.twig');
  21.     }
  22.     /*Function to create a new user*/
  23.     /**
  24.      * @Route("/subscribe", name="homesubscription", methods={"GET", "POST"})
  25.      */
  26.      public function newUser(Request $requestEntityManagerInterface $entityManagerUserPasswordHasherInterface $encoder): Response
  27.     {
  28.         $user = new User();
  29.         $form $this->createForm(UserType::class, $user);
  30.         $form->handleRequest($request);
  31.         if ($form->isSubmitted() && $form->isValid()) {
  32.             $user->setPassword($encoder->hashPassword($user$user->getPassword()));
  33.             /*When you create a new user I fix the status to 1 (active) and define the slug*/
  34.             $user->setStatus(1);
  35.             $user->setSlug($user->getFirstname());
  36.             
  37.             $avatarFile $form->get('picture')->getData();
  38.             //If there is there some data in the field picture, we treat them
  39.             if ($avatarFile != null) {
  40.                 $newFilename 'user'.uniqid().'.'.$avatarFile->guessExtension();
  41.                 // Move the file to the directory where avatars are stored
  42.                 try {
  43.                     $avatarFile->move(
  44.                         $this->getParameter('uploads_directory'),
  45.                         $newFilename
  46.                     );
  47.                 } catch (FileException $e) { 
  48.                     $this->addFlash('error''Une erreur est survenue. Essayer à nouveau');
  49.                     return $this->redirectToRoute('user_home', ['slug'=>$userInterface->getSlug()], Response::HTTP_SEE_OTHER); 
  50.                 }
  51.                 // We update the user class
  52.                 $user->setPicture($newFilename);
  53.             }         
  54.             $entityManager->persist($user);
  55.             $entityManager->flush();
  56.             return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  57.         }
  58.         /*display the form*/
  59.         return $this->renderForm('home/new.html.twig', [
  60.             'user' => $user,
  61.             'form' => $form,
  62.         ]);
  63.     }
  64.     
  65.     /**
  66.      * @Route("/contact", name="contactpage")
  67.      *
  68.      *
  69.      */
  70.     public function contact()
  71.     {
  72.         return $this->render('home/contact.html.twig');
  73.     }
  74.     /**
  75.      * @Route("/mentions-legales", name="legalnotice")
  76.      *
  77.      * @return void
  78.      */
  79.     public function legalNotice()
  80.     {
  81.         return $this->render('home/legal_notice.html.twig');
  82.     }
  83. }