-2

I made a registration page using php bin/make:registration-form and the default arguments/parameters, it worked but when I visit https://127.0.0.1:8000/register there is just a blank page, view source only shows the number 1 and nothing else. I'm not sure how I would even go about fixing this?

Here's the RegistrationController.php:

<?php

namespace App\Controller;

use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Security\EmailVerifier;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;

class RegistrationController extends AbstractController
{
    private EmailVerifier $emailVerifier;

    public function __construct(EmailVerifier $emailVerifier)
    {
        $this->emailVerifier = $emailVerifier;
    }

    #[Route('/register', name: 'app_register')]
    public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
    {
        $user = new User();
        $form = $this->createForm(RegistrationFormType::class, $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // encode the plain password
            $user->setPassword(
            $userPasswordHasher->hashPassword(
                    $user,
                    $form->get('plainPassword')->getData()
                )
            );

            $entityManager->persist($user);
            $entityManager->flush();

            // generate a signed url and email it to the user
            $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
                (new TemplatedEmail())
                    ->from(new Address('email_goes_here', 'Test Mail Bot Remorka'))
                    ->to($user->getEmail())
                    ->subject('Please Confirm your Email')
                    ->htmlTemplate('registration/**confirmation_email.html.twig**')
            );
            // do anything else you need here, like send an email

            return $this->redirectToRoute('api_app_remorque_list');
        }

        return $this->render('**registration/register.html.twig**', [
            'registrationForm' => $form->createView(),
        ]);
    }

    #[Route('/verify/email', name: 'app_verify_email')]
    public function verifyUserEmail(Request $request, TranslatorInterface $translator): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

        // validate email confirmation link, sets User::isVerified=true and persists
        try {
            $this->emailVerifier->handleEmailConfirmation($request, $this->getUser());
        } catch (VerifyEmailExceptionInterface $exception) {
            $this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));

            return $this->redirectToRoute('app_register');
        }

        // @TODO Change the redirect on success and handle or remove the flash message in your templates
        $this->addFlash('success', 'Your email address has been verified.');

        return $this->redirectToRoute('app_register');
    }
}

(I changed my email for privacy concerns)

And this is the twig template templates\registration\register.html.twig

{% extends 'base.html.twig' %}

{% block title %}Register{% endblock %}

{% block body %}
    {% for flash_error in app.flashes('verify_email_error') %}
        <div class="alert alert-danger" role="alert">{{ flash_error }}</div>
    {% endfor %}

    <h1>Register</h1>

    {{ form_start(registrationForm) }}
        {{ form_row(registrationForm.email) }}
        {{ form_row(registrationForm.plainPassword, {
            label: 'Password'
        }) }}
        {{ form_row(registrationForm.agreeTerms) }}

        <button type="submit" class="btn">Register</button>
    {{ form_end(registrationForm) }}
{% endblock %}

Using php 8.1.2 and the following packages in my composer.json:

"require": {
    "php": ">=7.2.5",
    "ext-ctype": "*",
    "ext-iconv": "*",
    "blackfireio/blackfire-symfony-meta": "^1.0",
    "doctrine/annotations": "^1.0",
    "doctrine/doctrine-bundle": "^2.6",
    "doctrine/doctrine-migrations-bundle": "^3.2",
    "doctrine/orm": "^2.12",
    "friendsofsymfony/rest-bundle": "^3.3",
    "pagerfanta/pagerfanta": "^3.6",
    "phpdocumentor/reflection-docblock": "^5.3",
    "phpstan/phpdoc-parser": "^1.5",
    "sensio/framework-extra-bundle": "^6.1",
    "symfony/asset": "5.4.*",
    "symfony/console": "5.4.*",
    "symfony/doctrine-messenger": "5.4.*",
    "symfony/dotenv": "5.4.*",
    "symfony/expression-language": "5.4.*",
    "symfony/flex": "^1.17|^2",
    "symfony/form": "5.4.*",
    "symfony/framework-bundle": "5.4.*",
    "symfony/google-mailer": "5.4.*",
    "symfony/http-client": "5.4.*",
    "symfony/intl": "5.4.*",
    "symfony/mailer": "5.4.*",
    "symfony/mime": "5.4.*",
    "symfony/monolog-bundle": "^3.0",
    "symfony/notifier": "5.4.*",
    "symfony/process": "5.4.*",
    "symfony/property-access": "5.4.*",
    "symfony/property-info": "5.4.*",
    "symfony/proxy-manager-bridge": "5.4.*",
    "symfony/runtime": "5.4.*",
    "symfony/security-bundle": "5.4.*",
    "symfony/serializer": "5.4.*",
    "symfony/string": "5.4.*",
    "symfony/translation": "5.4.*",
    "symfony/twig-bundle": "5.4.*",
    "symfony/validator": "5.4.*",
    "symfony/web-link": "5.4.*",
    "symfony/webapp-meta": "^1.0",
    "symfony/webpack-encore-bundle": "^1.12",
    "symfony/yaml": "5.4.*",
    "symfonycasts/verify-email-bundle": "^1.10",
    "symfonycorp/platformsh-meta": "^1.0",
    "twig/extra-bundle": "^2.12|^3.0",
    "twig/twig": 
Melek
  • 9
  • 5
  • Shouldn't this `'**registration/register.html.twig**'` be `'registration/register.html.twig'`. Not sure why you have asterisks? – Bossman May 23 '22 at 16:21
  • @Bossman in the real codes these asterisks aren't present, I was trying to format those as bold, I'm quite new to stackoverflow. – Melek May 23 '22 at 16:26
  • Update: I found out my mistake, there was a typo in the .env file and for some reason fixing that also fixed the /register page. – Melek May 23 '22 at 16:29

0 Answers0