【问题标题】:Symfony : Error: Cannot redeclare class Symfony\Component\Security\Core\User\UserInterface when validating a formSymfony:错误:验证表单时无法重新声明类 Symfony\Component\Security\Core\User\UserInterface
【发布时间】:2016-08-18 09:04:10
【问题描述】:

我正在使用 Symfony v3.1,在验证用于创建用户的表单时出现错误。

这是堆栈跟踪:

FatalErrorException in UserInterface.php line 34:
Error: Cannot redeclare class Symfony\Component\Security\Core\User\UserInterface
in UserInterface.php line 34
at Composer\Autoload\includeFile() in ClassLoader.php line 302
at ClassLoader->loadClass() in AnnotationRegistry.php line 145
at call_user_func:{/vagrant/ogam/website/htdocs/server/ogamServer/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php:145}() in AnnotationRegistry.php line 145
at AnnotationRegistry::loadAnnotationClass() in DocParser.php line 447
at DocParser->classExists() in DocParser.php line 707
at DocParser->Annotation() in DocParser.php line 641
at DocParser->Annotations() in DocParser.php line 334
at DocParser->parse() in AnnotationReader.php line 257
at AnnotationReader->getMethodAnnotations() in CachedReader.php line 151
at CachedReader->getMethodAnnotations() in AnnotationLoader.php line 74
at AnnotationLoader->loadClassMetadata() in LoaderChain.php line 57
at LoaderChain->loadClassMetadata() in LazyLoadingMetadataFactory.php line 127
at LazyLoadingMetadataFactory->getMetadataFor() in RecursiveContextualValidator.php line 339
at RecursiveContextualValidator->validateObject() in RecursiveContextualValidator.php line 154
at RecursiveContextualValidator->validate() in FormValidator.php line 51
at FormValidator->validate() in RecursiveContextualValidator.php line 842
at RecursiveContextualValidator->validateInGroup() in RecursiveContextualValidator.php line 553
at RecursiveContextualValidator->validateClassNode() in RecursiveContextualValidator.php line 359
at RecursiveContextualValidator->validateObject() in RecursiveContextualValidator.php line 154
at RecursiveContextualValidator->validate() in RecursiveValidator.php line 115
at RecursiveValidator->validate() in ValidationListener.php line 55

我的用户对象如下所示:

<?php
namespace MyBundle\Entity\Website;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use MyBundle\Entity\Website\Role as Role;
use MyBundle\Entity\Website\Provider as Provider;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * User.
 *
 * @ORM\Table(name="users", schema="website")
 * @ORM\Entity(repositoryClass="MyBundle\Repository\Website\UserRepository")
 * @UniqueEntity(fields="login", message="Login already taken")
 * @UniqueEntity(fields="email", message="Email already taken")
 */
class User implements UserInterface, \Serializable {

    /**
     * The login.
     *
     * @var string @Assert\Length(max=50)
     *      @Assert\NotBlank()
     *      @ORM\Column(name="user_login", type="string", length=50, nullable=false, unique=true)
     *      @ORM\Id
     */
    private $login;

    /**
     * The user name.
     *
     * @var string @ORM\Column(name="user_name", type="string", length=50, nullable=true)
     *      @Assert\Length(max=50)
     */
    private $username;

它是我的代码中唯一实现 UserInterface 的东西。我没有使用 FOSUserBundle。看起来,在验证表单时,注释解析器会尝试实例化数据对象及其父对象。

我尝试使用“$this->createFormBuilder($user, ...”在控制器中的函数中创建表单,我尝试使用 FormBuilderInterface 在单独的类中创建表单,但是两种情况都会出现问题。

我尝试设置“data_class”选项,但问题仍然存在。

我已经注释了表单中的大部分字段,只留下登录字段,但它仍然是一样的。

欢迎任何帮助...

【问题讨论】:

  • 提供帮助的信息很少。
  • 检查文件名,如果您使用 git,请确保您的文件名正确。我碰巧将原始文件放入Git时是Someinterface,然后重命名为SomeInterface。 Git 似乎不区分大小写,但 Php 是。
  • 此用户的发布表单类型。当你没有实现这个接口时会发生什么?
  • @Carlos git 区分大小写。但 Mac 不是。 ;-)
  • @dragoste 这对我来说很有启发性!!谢谢!!

标签: php forms validation symfony


【解决方案1】:

好吧,我的错。

问题出在用户实体的评论中。我使用了 @ 字符,它被作为注释读取。

    /**
     * Méthode à implémenter pour respecter @UserInterface.
     */
    public function getSalt() {
        // The algorithm doesn't require a separate salt.
        // You *may* need a real salt if you choose a different encoder.
        return null;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多