src/Controller/RegistrationController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Users;
  4. use App\Form\UsersType;
  5. use App\Security\LoginFormAuthenticator;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. class RegistrationController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/login", name="home_log")
  16.      * @Route("/register", name="home_reg")
  17.      */
  18.     public function index(Users $users nullRequest $requestEntityManagerInterface $managerUserPasswordEncoderInterface $encoderGuardAuthenticatorHandler $guardHandler,LoginFormAuthenticator $authenticator  )
  19.     {
  20. //       dd($request->server->get('HTTP_REFERER'));
  21.         $oripath $request->server->get('HTTP_REFERER');
  22.         
  23.         if(!$users){
  24.             $users = new Users();
  25.         }
  26.         $form $this->createForm(UsersType::class, $users);
  27.         $form->handleRequest($request);
  28.      
  29.       //   dd( $form );
  30.         if($form->isSubmitted() && $form->isValid()){     
  31.             $users->setRoles( [ 'ROLE_USER' ] );
  32.            
  33.             $hash$encoder->encodePassword($users$users->getPassword());
  34.             $users->setPassword($hash);
  35.            
  36.             $manager->persist($users);
  37.             $manager->flush();
  38.           //  return $this->redirectToRoute($oripath);
  39.           return $guardHandler->authenticateUserAndHandleSuccess(
  40.             $users,
  41.             $request,
  42.             $authenticator,
  43.             'main' // firewall name in security.yaml 
  44.         );
  45.           return $this->redirect($oripath);
  46.         }
  47.         $errors $form->getErrors(true); 
  48.         $error $errors->__toString();
  49.         
  50.         $user $this->getUser();
  51.         if( $request->get('_route') == 'home_log'){
  52.             $showform 1;
  53.         }else if( $request->get('_route') == 'home_reg'){
  54.             $showform 2;
  55.         }else{
  56.             $showform 0;
  57.         } 
  58.         
  59.         return $this->render('registration/index.html.twig', [
  60.             'user' => $user,
  61.             'form' => $form->createView(),
  62.             'errors' => $error,
  63.             'showform' => $showform
  64.         ]);
  65.     }
  66. }