If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeAhora estamos generando una URL firmada que normalmente incluiríamos en un correo electrónico de "confirmación de la dirección de correo electrónico" que enviamos al usuario tras el registro. Para simplificar las cosas, sólo vamos a renderizar esa URL en la página después del registro.
Vamos a ver qué aspecto tiene. Refresca y... ¡ah! ¡Un error de aspecto terrible!
Se ha configurado un enlace para un argumento llamado
$formLoginAuthenticator
en_defaults
, pero no se ha encontrado el argumento correspondiente.
Así que, hasta hace unos minutos, teníamos un argumento para nuestra acción register()
que se llamaba $formLoginAuthenticator
. En config/services.yaml
, hemos configurado un "bind" global que decía
Siempre que un servicio autocableado tenga un argumento llamado
$formLoginAuthenticator
, por favor, pasa este servicio.
... lines 1 - 8 | |
services: | |
# default configuration for services in *this* file | |
_defaults: | |
... lines 12 - 13 | |
bind: | |
... line 15 | |
$formLoginAuthenticator: '@security.authenticator.form_login.main' | |
... lines 17 - 32 |
Una de las cosas buenas de bind es que si no hay un argumento que coincida en ninguna parte de nuestra aplicación, lanza una excepción. Intenta asegurarse de que no estamos cometiendo una errata accidental.
En nuestra situación, ya no necesitamos ese argumento. Así que elimínalo. Y ahora... ¡nuestra página de registro está viva!
¡Hagamos esto! Introduce un correo electrónico, una contraseña, acepta las condiciones y pulsa registrar. ¡Genial! Aquí está nuestra URL de confirmación por correo electrónico. Puedes ver que va a/verify
: que dará a nuestra nueva acción verifyUserEmail()
. También incluye una caducidad. Eso es algo que puedes configurar... es el tiempo de validez del enlace. Y tiene un signature
: que es algo que ayudará a demostrar que el usuario no se ha inventado esta URL: definitivamente viene de nosotros.
También incluye un id=18
: nuestro identificador de usuario.
Así que nuestro trabajo ahora es ir al método del controlador verifyUserEmail
aquí abajo y validar esa URL firmada. Para ello, necesitamos unos cuantos argumentos: el objeto Request
-para poder leer los datos de la URL-, unVerifyEmailHelperInterface
para ayudarnos a validar la URL y, por último, nuestro UserRepository
-para poder consultar el objeto User
:
... lines 1 - 6 | |
use App\Repository\UserRepository; | |
... line 8 | |
use Symfony\Component\HttpFoundation\Request; | |
... lines 10 - 13 | |
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface; | |
class RegistrationController extends AbstractController | |
{ | |
... lines 18 - 60 | |
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository): Response | |
{ | |
... lines 63 - 80 | |
} | |
} |
Y en realidad, ese es nuestro primer trabajo. Digamos que $user = $userRepository->find()
y encontrar el usuario al que pertenece este enlace de confirmación leyendo el parámetro de consulta id
. Así que, $request->query->get('id')
. Y si, por alguna razón, no podemos encontrar el User
, vamos a lanzar una página 404 lanzando$this->createNotFoundException()
:
... lines 1 - 15 | |
class RegistrationController extends AbstractController | |
{ | |
... lines 18 - 60 | |
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository): Response | |
{ | |
$user = $userRepository->find($request->query->get('id')); | |
if (!$user) { | |
throw $this->createNotFoundException(); | |
} | |
... lines 67 - 80 | |
} | |
} |
Ahora podemos asegurarnos de que la URL firmada no ha sido manipulada. Para ello, añade un bloque try-catch. Dentro, di $verifyEmailHelper->validateEmailConfirmation()
y pasa un par de cosas. Primero, la URL firmada, que... es la URL actual. Obténla con $request->getUri()
. A continuación, pasa el identificador del usuario - $user->getId()
y luego el correo electrónico del usuario - $user->getEmail()
:
... lines 1 - 15 | |
class RegistrationController extends AbstractController | |
{ | |
... lines 18 - 60 | |
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository): Response | |
{ | |
$user = $userRepository->find($request->query->get('id')); | |
if (!$user) { | |
throw $this->createNotFoundException(); | |
} | |
try { | |
$verifyEmailHelper->validateEmailConfirmation( | |
$request->getUri(), | |
$user->getId(), | |
$user->getEmail(), | |
); | |
... lines 74 - 77 | |
} | |
... lines 79 - 80 | |
} | |
} |
Esto asegura que la identificación y el correo electrónico no han cambiado en la base de datos desde que se envió el correo de verificación. Bueno, el id definitivamente no ha cambiado... ya que lo acabamos de utilizar para la consulta. Esta parte sólo se aplica realmente si confías en que el usuario esté conectado para verificar su correo electrónico.
De todos modos, si esto tiene éxito... ¡no pasará nada! Si falla, lanzará una excepción especial que implementa VerifyEmailExceptionInterface
:
... lines 1 - 15 | |
class RegistrationController extends AbstractController | |
{ | |
... lines 18 - 60 | |
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository): Response | |
{ | |
... lines 63 - 67 | |
try { | |
$verifyEmailHelper->validateEmailConfirmation( | |
$request->getUri(), | |
$user->getId(), | |
$user->getEmail(), | |
); | |
} catch (VerifyEmailExceptionInterface $e) { | |
... lines 75 - 77 | |
} | |
... lines 79 - 80 | |
} | |
} |
Así que, aquí abajo, sabemos que la verificación de la URL ha fallado... tal vez alguien se ha equivocado. O, más probablemente, el enlace ha caducado. Digamos al usuario la razón aprovechando de nuevo el sistema flash. Digamos $this->addFlash()
, pero esta vez poniéndolo en una categoría diferente llamada error
. Luego, para decir lo que ha ido mal, utiliza $e->getReason()
. Por último, utiliza redirectToRoute()
para enviarlos a algún sitio. ¿Qué tal la página de registro?
... lines 1 - 15 | |
class RegistrationController extends AbstractController | |
{ | |
... lines 18 - 60 | |
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository): Response | |
{ | |
... lines 63 - 67 | |
try { | |
$verifyEmailHelper->validateEmailConfirmation( | |
$request->getUri(), | |
$user->getId(), | |
$user->getEmail(), | |
); | |
} catch (VerifyEmailExceptionInterface $e) { | |
$this->addFlash('error', $e->getReason()); | |
return $this->redirectToRoute('app_register'); | |
} | |
dd('TODO'); | |
} | |
} |
Para mostrar el error, vuelve a base.html.twig
, duplica todo este bloque, pero busca los mensajes de error
y utiliza alert-danger
:
<html> | |
... lines 3 - 14 | |
<body | |
... lines 16 - 81 | |
{% for flash in app.flashes('success') %} | |
<div class="alert alert-success">{{ flash }}</div> | |
{% endfor %} | |
{% for flash in app.flashes('error') %} | |
<div class="alert alert-danger">{{ flash }}</div> | |
{% endfor %} | |
... lines 88 - 92 | |
</body> | |
</html> |
¡Uf! Probemos el caso del error. Copia la URL y luego abre una nueva pestaña y pégala. Si voy a esta URL real... funciona. Bueno, todavía tenemos que hacer algo más de codificación, pero llega a nuestro TODO en la parte inferior del controlador. Ahora juega con la URL, como eliminar algunos caracteres... o ajustar la caducidad o cambiar el id
. Ahora... ¡sí! Ha fallado porque nuestro enlace no es válido. Si el enlace estuviera caducado, verías un mensaje al respecto.
Así que, por fin, ¡acabemos con el caso feliz! En la parte inferior de nuestro controlador, ahora que sabemos que el enlace de verificación es válido, hemos terminado. Para nuestra aplicación, podemos decir $user->isVerified(true)
y almacenarlo en la base de datos:
... lines 1 - 16 | |
class RegistrationController extends AbstractController | |
{ | |
... lines 19 - 61 | |
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository, EntityManagerInterface $entityManager): Response | |
{ | |
... lines 64 - 68 | |
try { | |
... lines 70 - 78 | |
} | |
$user->setIsVerified(true); | |
... lines 82 - 86 | |
} | |
} |
Veamos... necesitamos un argumento más: EntityManagerInterface $entityManager
:
... lines 1 - 7 | |
use Doctrine\ORM\EntityManagerInterface; | |
... lines 9 - 16 | |
class RegistrationController extends AbstractController | |
{ | |
... lines 19 - 61 | |
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository, EntityManagerInterface $entityManager): Response | |
{ | |
... lines 64 - 86 | |
} | |
} |
Aquí abajo, utiliza $entityManager->flush()
para guardar ese cambio:
... lines 1 - 16 | |
class RegistrationController extends AbstractController | |
{ | |
... lines 19 - 61 | |
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository, EntityManagerInterface $entityManager): Response | |
{ | |
... lines 64 - 80 | |
$user->setIsVerified(true); | |
$entityManager->flush(); | |
... lines 83 - 86 | |
} | |
} |
Y demos a esto un feliz mensaje de éxito:
¡Cuenta verificada! Ya puedes conectarte.
Bueno, la verdad es que todavía no impedimos que se conecten antes de verificar su correo electrónico. Pero lo haremos pronto. De todos modos, termina redirigiendo a la página de inicio de sesión: app_login
:
... lines 1 - 16 | |
class RegistrationController extends AbstractController | |
{ | |
... lines 19 - 61 | |
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository, EntityManagerInterface $entityManager): Response | |
{ | |
... lines 64 - 80 | |
$user->setIsVerified(true); | |
$entityManager->flush(); | |
$this->addFlash('success', 'Account Verified! You can now log in.'); | |
return $this->redirectToRoute('app_login'); | |
} | |
} |
Si quieres ser aún más genial, podrías autenticar manualmente al usuario de la misma manera que lo hicimos antes en nuestro controlador de registro. Eso está totalmente bien y depende de ti.
De vuelta a mi pestaña principal... copia ese enlace de nuevo, pégalo y... ¡estamos verificados! ¡Qué bien!
Lo único que queda por hacer es impedir que el usuario se registre hasta que haya verificado su correo electrónico. Para ello, primero tenemos que conocer los eventos que ocurren dentro del sistema de seguridad. Y para mostrarlos, aprovecharemos una nueva función muy interesante: el estrangulamiento del inicio de sesión.
Hey Juan E.!
Hmm, that's a mystery! And you've asked the correct questions: how to debug / find out what happened during that redirection.
Here's what I like to do:
A) In config/packages/dev/web_profiler.yaml
, you should have a intercept_redirects: false
line. Change that to intercept_redirects: true
.
B) Now, whenever Symfony redirects you, instead of redirecting, you will see a page that says something like "About to redirect you to http://...". But it hasn't actually redirected you.
How is that useful? Because now you can do this:
1) Click the link, which will hit this URL, submit the form and (should) authenticate the user (make sure you put your redirectToRoute() back)
2) But, instead of redirecting, it will stop and show you the message. The useful thing is that, on this screen, you will see the web debug toolbar for this page. The question is: do you see yourself as authenticated in the web debug toolbar?
The answer to this question will tell us where to look next. If you ARE authenticated (but then when you click the link to "follow the redirect" suddenly you are not authenticated), then you are "losing" authentication, probably because some data on your User object is being seen as "changed" (if you have this situation, I can tell you more - it is common if you, for some reason, implement a custom \Serializable interface on your User class, which you probably shouldn't have).
Let me know what you find out.
Cheers!
Hi I just managed to make it work using the rememberMe Handler Interface and adding this line before the redirect:
$rememberMe->createRememberMeCookie($this->getUser());
Hi, as I said in the previous reply I still get "kicked out" this is my User.php entity, I wonder if the libPhoneNumber object could be the problem.
`<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Adamski\Symfony\PhoneNumberBundle\Model\PhoneNumber;
use Adamski\Symfony\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
/**
const REGISTERED_SUCCESFULLY = 'Se ha registrado exitosamente';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Groups("main")
*
* @Assert\NotBlank()
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="json")
* @Groups("main")
*
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string", nullable=true)
* @Assert\NotBlank()
*/
private $password;
/**
* @ORM\Column(type="string", length=2, nullable=true)
* @Groups("main")
*/
private $langue;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups("main")
* @Assert\NotBlank()
*/
private $nom;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups("main")
* @Assert\NotBlank()
*/
private $prenom;
/**
* @Groups("main")
* @AssertPhoneNumber
* @ORM\Column(name="telephone", type="phone_number", nullable=true)
*/
private $telephone;
/**
* @ORM\Column(type="string", length=15, nullable=true)
* @Groups("main")
*/
private $position;
/**
* @ORM\Column(type="datetime")
* @Groups("main")
*/
private $date_ajout;
/**
* @ORM\ManyToMany(targetEntity=Dates::class, inversedBy="users")
*/
private $date;
/**
* @ORM\OneToMany(targetEntity=Reservation::class, mappedBy="user", cascade={"persist","remove"})
*/
private $reservation;
/**
* @ORM\OneToMany(targetEntity=BlogTranslation::class, mappedBy="user", orphanRemoval=true)
*/
private $blogTranslations;
/**
* @ORM\OneToMany(targetEntity=Document::class, mappedBy="user")
*/
private $documents;
/**
* @ORM\OneToMany(
* targetEntity=Travellers::class,
* mappedBy="user",
* fetch="EXTRA_LAZY",
* orphanRemoval=true,
* cascade={"persist"}
* )
*/
private $travellers;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $address;
/**
* @ORM\Column(type="string", length=6, nullable=true)
*/
private $postcode;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $city;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $country;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $nationality;
/**
* @ORM\Column(type="string", length=3, nullable=true)
*/
private $sizes;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $verification;
/**
* @ORM\OneToMany(targetEntity=ReservationData::class, mappedBy="User", orphanRemoval=true)
*/
private $reservationData;
/**
* @ORM\OneToMany(targetEntity=Codespromo::class, mappedBy="user")
*/
private $codespromos;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
public function __construct()
{
$this->date = new ArrayCollection();
$this->reservation = new ArrayCollection();
$this->blogTranslations = new ArrayCollection();
$this->documents = new ArrayCollection();
$this->travellers = new ArrayCollection();
$this->reservations = new ArrayCollection();
$this->reservationData = new ArrayCollection();
$this->codespromos = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string)$this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getLangue(): ?string
{
return $this->langue;
}
public function setLangue(string $langue): self
{
$this->langue = $langue;
return $this;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getPrenom(): ?string
{
return $this->prenom;
}
public function setPrenom(string $prenom): self
{
$this->prenom = $prenom;
return $this;
}
public function getTelephone(): ?PhoneNumber
{
return $this->telephone;
}
public function setTelephone(?PhoneNumber $telephone): self
{
$this->telephone = $telephone;
return $this;
}
public function getPosition(): ?string
{
return $this->position;
}
public function setPosition(?string $position): self
{
$this->position = $position;
return $this;
}
public function getDateAjout(): ?\DateTimeInterface
{
return $this->date_ajout;
}
public function setDateAjout(\DateTimeInterface $date_ajout): self
{
$this->date_ajout = $date_ajout;
return $this;
}
/**
* @return Collection|Dates[]
*/
public function getDate(): Collection
{
return $this->date;
}
public function addDate(Dates $date): self
{
if (!$this->date->contains($date)) {
$this->date[] = $date;
}
return $this;
}
public function removeDate(Dates $date): self
{
$this->date->removeElement($date);
return $this;
}
/**
* @return Collection|Reservations[]
*/
public function getReservation(): Collection
{
return $this->reservation;
}
public function addReservation(Reservation $reservation): self
{
if (!$this->reservation->contains($reservation)) {
$this->reservation[] = $reservation;
}
return $this;
}
public function removeReservation(Reservation $reservation): self
{
$this->reservation->removeElement($reservation);
return $this;
}
/**
* @return Collection|BlogTranslation[]
*/
public function getBlogTranslations(): Collection
{
return $this->blogTranslations;
}
public function addBlogTranslation(BlogTranslation $blogTranslation): self
{
if (!$this->blogTranslations->contains($blogTranslation)) {
$this->blogTranslations[] = $blogTranslation;
$blogTranslation->setUser($this);
}
return $this;
}
public function removeBlogTranslation(BlogTranslation $blogTranslation): self
{
if ($this->blogTranslations->removeElement($blogTranslation)) {
// set the owning side to null (unless already changed)
if ($blogTranslation->getUser() === $this) {
$blogTranslation->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Document[]
*/
public function getDocuments(): Collection
{
return $this->documents;
}
public function addDocument(Document $document): self
{
if (!$this->documents->contains($document)) {
$this->documents[] = $document;
$document->setUser($this);
}
return $this;
}
public function removeDocument(Document $document): self
{
if ($this->documents->removeElement($document)) {
// set the owning side to null (unless already changed)
if ($document->getUser() === $this) {
$document->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Travellers[]
*/
public function getTravellers(): Collection
{
return $this->travellers;
}
public function addTraveller(Travellers $traveller): self
{
if (!$this->travellers->contains($traveller)) {
$this->travellers[] = $traveller;
$traveller->setUser($this);
}
return $this;
}
public function removeTraveller(Travellers $traveller): self
{
if ($this->travellers->removeElement($traveller)) {
// set the owning side to null (unless already changed)
if ($traveller->getUser() === $this) {
$traveller->setUser(null);
}
}
return $this;
}
public function __toString(): string
{
return $this->getUserIdentifier();
}
/**
* @return Collection|Reservation[]
*/
public function getReservations(): Collection
{
return $this->reservation;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
public function getPostcode(): ?string
{
return $this->postcode;
}
public function setPostcode(?string $postcode): self
{
$this->postcode = $postcode;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(?string $city): self
{
$this->city = $city;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(?string $country): self
{
$this->country = $country;
return $this;
}
public function getNationality(): ?string
{
return $this->nationality;
}
public function setNationality(?string $nationality): self
{
$this->nationality = $nationality;
return $this;
}
public function getSizes(): ?string
{
return $this->sizes;
}
public function setSizes(?string $sizes): self
{
$this->sizes = $sizes;
return $this;
}
public function getVerification(): ?string
{
return $this->verification;
}
public function setVerification(?string $verification): self
{
$this->verification = $verification;
return $this;
}
/**
* @return Collection|ReservationData[]
*/
public function getReservationData(): Collection
{
return $this->reservationData;
}
public function addReservationData(ReservationData $reservationData): self
{
if (!$this->reservationData->contains($reservationData)) {
$this->reservationData[] = $reservationData;
$reservationData->setUser($this);
}
return $this;
}
public function removeReservationData(ReservationData $reservationData): self
{
if ($this->reservationData->removeElement($reservationData)) {
// set the owning side to null (unless already changed)
if ($reservationData->getUser() === $this) {
$reservationData->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Codespromo[]
*/
public function getCodespromos(): Collection
{
return $this->codespromos;
}
public function addCodespromo(Codespromo $codespromo): self
{
if (!$this->codespromos->contains($codespromo)) {
$this->codespromos[] = $codespromo;
$codespromo->setUser($this);
}
return $this;
}
public function removeCodespromo(Codespromo $codespromo): self
{
if ($this->codespromos->removeElement($codespromo)) {
// set the owning side to null (unless already changed)
if ($codespromo->getUser() === $this) {
$codespromo->setUser(null);
}
}
return $this;
}
public function getIsVerified(): ?bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
}`
Hi thank you, I have proceeded following your indications and so far I find my self in the place we expected. By preventing direct redirection I got to that "intermediate" page and the user was still logged in, but after redirection it disappeared. Since the project I am managing "inherits" many elements from a former project I will clean up the data model of the user class and then test again, if I get no progress I may send here the User.php entity class with the hope you may find something I couldn't, I will try tomorrow and whatever the outcome I will post it here. Thanks so much.
Hey Juan!
I see that you got this working, I just wanted to apologize for not replying - I'm not sure how I lost this message! Overall, your solution is probably fine. If you'd like to learn more about the underlying issue and solution, you can read about it on this thread: https://symfonycasts.com/sc...
Cheers!
Hey Ruslan,
I think you need to translate the exact text if the error is from core Symfony. If the error message is your custom - you can use a translation message key, translate it in translation files, and use the translator in that spot to translate.
I hope this helps!
Cheers!
Hi Victor,
In this tutorial, we have changed Error messages for Login form (lesson 10) in translations\security.en.yaml. As I understand "security" is domain and I can see it in debug bar "Translation".
$e->getReason() is from VerifyEmailExceptionInterface. - but here I miss understand how to translate (I mean translation file name)
Thank you.
Looks like I understand my fault.
in this code I need to pass $error
-----
} catch (VerifyEmailExceptionInterface $e){
$this->addFlash('error', $e->getReason());
return $this->redirectToRoute('app_register');
}
-----
and in twig use the same construction like in login.html.twig.
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
Am I right?
Hey Ruslan,
I'm not sure you can pass the error object, IIRC you can only pass string to the addFlash() method. So, either you need to translate the message in PHP using trans() method before pass the string to the addFlash(), or you can pass the untranslated string to the addFlash() and translate it later in the template, i.e. "<div class="alert alert-success">{{ flash|trans }}</div>" as you mentioned, but yeah, if the message requires some messageData - it you do need to pass it into the template somehow, so probably better to translate in PHP where you have access to the object.
But if it's possible to pass the object - yeah, you can try your solution as well then. But you would need one more check in the template - you would need to know if you passed a string or an object and so render it a bit differently.
Cheers!
// composer.json
{
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^3.3", // v3.3.0
"composer/package-versions-deprecated": "^1.11", // 1.11.99.4
"doctrine/annotations": "^1.0", // 1.13.2
"doctrine/doctrine-bundle": "^2.1", // 2.6.3
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.1.1
"doctrine/orm": "^2.7", // 2.10.1
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"knplabs/knp-time-bundle": "^1.11", // v1.16.1
"pagerfanta/doctrine-orm-adapter": "^3.3", // v3.3.0
"pagerfanta/twig": "^3.3", // v3.3.0
"phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
"scheb/2fa-bundle": "^5.12", // v5.12.1
"scheb/2fa-qr-code": "^5.12", // v5.12.1
"scheb/2fa-totp": "^5.12", // v5.12.1
"sensio/framework-extra-bundle": "^6.0", // v6.2.0
"stof/doctrine-extensions-bundle": "^1.4", // v1.6.0
"symfony/asset": "5.3.*", // v5.3.4
"symfony/console": "5.3.*", // v5.3.7
"symfony/dotenv": "5.3.*", // v5.3.8
"symfony/flex": "^1.3.1", // v1.17.5
"symfony/form": "5.3.*", // v5.3.8
"symfony/framework-bundle": "5.3.*", // v5.3.8
"symfony/monolog-bundle": "^3.0", // v3.7.0
"symfony/property-access": "5.3.*", // v5.3.8
"symfony/property-info": "5.3.*", // v5.3.8
"symfony/rate-limiter": "5.3.*", // v5.3.4
"symfony/runtime": "5.3.*", // v5.3.4
"symfony/security-bundle": "5.3.*", // v5.3.8
"symfony/serializer": "5.3.*", // v5.3.8
"symfony/stopwatch": "5.3.*", // v5.3.4
"symfony/twig-bundle": "5.3.*", // v5.3.4
"symfony/ux-chartjs": "^1.3", // v1.3.0
"symfony/validator": "5.3.*", // v5.3.8
"symfony/webpack-encore-bundle": "^1.7", // v1.12.0
"symfony/yaml": "5.3.*", // v5.3.6
"symfonycasts/verify-email-bundle": "^1.5", // v1.5.0
"twig/extra-bundle": "^2.12|^3.0", // v3.3.3
"twig/string-extra": "^3.3", // v3.3.3
"twig/twig": "^2.12|^3.0" // v3.3.3
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
"symfony/debug-bundle": "5.3.*", // v5.3.4
"symfony/maker-bundle": "^1.15", // v1.34.0
"symfony/var-dumper": "5.3.*", // v5.3.8
"symfony/web-profiler-bundle": "5.3.*", // v5.3.8
"zenstruck/foundry": "^1.1" // v1.13.3
}
}
Hello I have tried a slightly different approach. I actually have created a verification system which sends a random code via email and following your reccomendation in this chapter "If you wanted to be even cooler, you could manually authenticate the user in the same way that we did earlier in our registration controller. That's totally ok and up to you." I have tried to authenticate the user:
My security.yaml file:
After the redirection $this->getUser() returns null thus the session is not mantained, so seems that validation did not work or at least the session was not retained and I would need to send the verified user to login which is possible but not desired in my case. How can I find out what has happened during that redirection? What would be the best debugging strategy?