我目前遇到了Symfony 4的一个非常令人困惑的问题(或者甚至不是,我不知道)。我手动设置了登录/注册系统,到目前为止一直运行良好。我已经开发了一个可以为其主要公司创建分支的表单。我以一种确切的方式使用了 FormType :
<?php
// src/Form/UserType.php
namespace App\Form;
use App\Entity\Filiale;
use App\Entity\Kammer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class FilialType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('apothekenname', TextType::class)
->add('email', EmailType::class)
->add('anrede', TextType::class)
->add('titel', TextType::class, array(
'required' => false
))
->add('vorname', TextType::class)
->add('name', TextType::class)
->add('adresszusatz', TextType::class, array(
'required' => false
))
->add('strasse', TextType::class)
->add('hausnummer', TextType::class)
->add('plz', NumberType::class)
->add('ort', TextType::class)
//->add('kammer', TextType::class)
->add('kammer', EntityType::class, array(
'class' => Kammer::class,
'choice_label' => 'name',
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Filiale::class,
));
}
}
?>
呈现FormType的视图如下所示,其中有一个单独的按钮触发Bootstrap模式对话框:
<div class="modal fade" id="modal-add-filiale" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Neue Filialapotheke anlegen</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Schließen">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ form_start(form_add_filiale) }}
<div class="form-group row">
<label class="col-xl-3" for="filiale-apothekenname">Apothekenname</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.apothekenname) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-anrede">Anrede</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.anrede) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-titel">Titel</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.titel) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-vorname">Vorname</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.vorname) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-name">Name</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.name) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-strasse">Straße</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.strasse) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-hausnummer">Hausnummer</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.hausnummer) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-adresszusatz">Adresszusatz</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.adresszusatz) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-plz">PLZ</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.plz) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-stadt">Stadt</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.ort) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-email">Email</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.email) }}
</div>
</div>
<div class="form-group row">
<label class="col-xl-3" for="filiale-kammerbezirk">Kammerbezirk</label>
<div class="col-xl-9">
{{ form_widget(form_add_filiale.kammer) }}
</div>
</div>
<button type="submit">Hu</button>
{{ form_end(form_add_filiale) }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Abbrechen</button>
<button id="modal-add-filiale-save" type="submit" class="btn btn-primary">Speichern</button>
</div>
</div>
</div>
</div>
处理表单,渲染和提交的控制器功能如下所示:
public function login(Request $request, AuthenticationUtils $authUtils, AuthorizationCheckerInterface $authChecker) {
$isLoggedIn = ($authChecker->isGranted('ROLE_USER') || $authChecker->isGranted('ROLE_USER_MO'));
if($isLoggedIn) {
$filiale = new Filiale();
$form = $this->createForm(FilialType::class, $filiale);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$filiale->setUser($this->getUser());
$em = $this->getDoctrine()->getManager();
$em->persist($filiale);
$em->flush();
return $this->render('default/startpage_loggedin.html.twig', array(
'breadcrumb' => 'BROTKRUMEN',
'form_add_filiale' => $form->createView(),
'toast_finished' => 1,
));
}
return $this->render('default/startpage_loggedin.html.twig', array(
'breadcrumb' => 'BROTKRUMEN',
'form_add_filiale' => $form->createView(),
));
}
else {
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('default/startpage_loggedout.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
}
虽然代码没有完美排列,而且我可能错过了一些最佳实践,但我仍然发现我面临的问题很奇怪。只有在登录时才能访问表单 - 当我单击按钮打开模式时,然后填写表单然后按提交按钮,我自动退出并显示错误消息&#34;凭据无效& #34;显示。
但是,当我将表单呈现和表单处理外包给新视图和新控制器功能时,代码完全按照它应该执行的操作,而不会触及登录用户。< / p>
我基本上没有想法,并且会感谢你们提供的任何暗示。提前谢谢!
编辑:Security.yml
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
encoders:
App\Entity\User:
algorithm: bcrypt
legacy_encoder:
algorithm: md5
encode_as_base64: false
iterations: 1
providers:
in_memory: { memory: ~ }
db_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
#pattern: ^/
#http_basic: ~
anonymous: ~
provider: db_provider
user_checker: App\Security\UserChecker
logout:
path: /logout
target: /
form_login:
login_path: home
check_path: home
access_control:
- { path: ^/motivwelten, roles: ROLE_USER }
- { path: ^/services/.*, roles: ROLE_USER }
- { path: ^/shop, roles: ROLE_USER }
- { path: ^/shop/.*, roles: ROLE_USER }
erase_credentials: false
答案 0 :(得分:0)
虽然不是我问过的最初问题的直接解决方案,但我通过移动每个逻辑来修复问题,但登录远离我控制器中的登录方法。
这应该是更好的做法。我相信提交到/ login route tirggers登录事件,并且由于没有凭据,服务无法验证用户并在显示“无效凭据”时终止会话。