<?php
// src/Controller/RegistrationController.php
namespace App\Controller;
use App\Entity\AppUser;
use App\Form\RegistrationFormType;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
/**
* @Route("/{_locale}/register")
*/
class RegistrationController extends AbstractController
{
/**
* @Route("/", name="registration_register")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function register(Request $request, UserPasswordHasherInterface $passwordHasher)
{
// ... e.g. get the user data from a registration form
$user = new AppUser;
$plaintextPassword = '';
$em = $this->getDoctrine()
->getManager();
$form = $this->createForm(RegistrationFormType::class, $user);
$error_message = '';
if($request->getMethod() == 'POST'){
$form->handleRequest($request);
if($form->isValid()){
$data = $form->getData();
$userExist = $em->getRepository(AppUser::class)
->findOneByEmail($data->getEmail());
if($userExist != null){
$error_message = 'Email adresse déjà utilisé';
}else{
$userExist = $em->getRepository(AppUser::class)
->findOneByUsername($data->getUsername());
if($userExist != null){
$error_message = 'Nom d\'utilisateur adresse déjà utilisé';
} else{
$plaintextPassword = '';
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword($user, $data->getPlainPassword());
$user->setPassword($hashedPassword);
$em->persist($user);
$em->flush();
return $this->redirectToRoute('registration_confirmed', array('username' => $user->getUsername()));
}
}
}
}
return $this->render('registration/register.html.twig', [
'form' => $form->createView(),
'error_message' => $error_message
]);
}
/**
* @Route("/registration-confirmed/{username}", name="registration_confirmed")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function registrationConfirmedAction(Request $request, UserPasswordHasherInterface $passwordHasher, AppUser $user)
{
// ... e.g. get the user data from a registration form
return $this->render('registration/confirmed.html.twig', [
'user' => $user
]);
}
}