我正在关注本教程: -
https://symfony.com/doc/current/security/entity_provider.html
https://symfony.com/doc/current/doctrine/registration_form.html
我进行了注册页面,但是当我尝试访问/注册页面时,会弹出安全区域警报。
以下是我的文件:
security.yaml
# config/packages/security.yaml
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: App\Entity\User
firewalls:
main:
pattern: ^/
http_basic: ~
provider: our_db_provider
UserRegistrationController.php
namespace App\Controller;
use App\Form\UserType;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class UserRegistrationController extends Controller {
/**
* @Route("/registration", name="user_registration")
*/
public function registerUser(Request $request, UserPasswordEncoderInterface $passwordEncoder) {
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if($form->isSubmitted and $form->isValid()) {
$password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('user_registration_successful');
}
return $this->render(
'Form/UserRegistration.html.twig',
array('form' => $form->createView())
);
}
}
UserType.php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class UserType extends AbstractType {
public function createForm (FormBuilderInterface $form, array $options) {
$form
->add('username', TextType::class)
->add('email', EmailType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label', 'Password'),
'second_options' => array('label', 'Repeat password')
));
}
public function configureOptions (OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => User::class
));
}
}
UserRegistrationForm.html.twig
{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.email) }}
{{ form_row(form.plainPassword.first) }}
{{ form_row(form.plainPassword.second) }}
<button type="submit">Register!</button>
{{ form_end(form) }}