<?php
namespace App\Controller;
use App\Entity\Users;
use App\Form\UsersType;
use App\Security\LoginFormAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class RegistrationController extends AbstractController
{
/**
* @Route("/login", name="home_log")
* @Route("/register", name="home_reg")
*/
public function index(Users $users = null, Request $request, EntityManagerInterface $manager, UserPasswordEncoderInterface $encoder, GuardAuthenticatorHandler $guardHandler,LoginFormAuthenticator $authenticator )
{
// dd($request->server->get('HTTP_REFERER'));
$oripath = $request->server->get('HTTP_REFERER');
if(!$users){
$users = new Users();
}
$form = $this->createForm(UsersType::class, $users);
$form->handleRequest($request);
// dd( $form );
if($form->isSubmitted() && $form->isValid()){
$users->setRoles( [ 'ROLE_USER' ] );
$hash= $encoder->encodePassword($users, $users->getPassword());
$users->setPassword($hash);
$manager->persist($users);
$manager->flush();
// return $this->redirectToRoute($oripath);
return $guardHandler->authenticateUserAndHandleSuccess(
$users,
$request,
$authenticator,
'main' // firewall name in security.yaml
);
return $this->redirect($oripath);
}
$errors = $form->getErrors(true);
$error = $errors->__toString();
$user = $this->getUser();
if( $request->get('_route') == 'home_log'){
$showform = 1;
}else if( $request->get('_route') == 'home_reg'){
$showform = 2;
}else{
$showform = 0;
}
return $this->render('registration/index.html.twig', [
'user' => $user,
'form' => $form->createView(),
'errors' => $error,
'showform' => $showform
]);
}
}