Symfony __PHP_Incomplete_Class无法转换为字符串

时间:2018-07-24 07:26:56

标签: php symfony login doctrine

提交登录表单时,出现该错误。我读过here,这可能是由于尝试

  

反序列化对象,但没有为该对象加载类

但是,因为代码隐藏在symfony内部,所以我不知道为什么在该实例中不会加载类。 Profiler似乎说没有执行任何查询,但是如果我输入了错误的凭据,它将显示“无效的凭据”,因此它必须正确地访问数据库。

用户类别

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity("email", message="This email is already in use.")
* @UniqueEntity("username", message="This username is already in use")
*/
class User implements UserInterface, \Serializable
{

private $roles = "ROLE_USER";

/**
 * @ORM\Column(name="salt",type="string", length=255)
 */
private $salt = "saltyboye";


/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(name="username",type="string", length=255, unique=true)
 */
private $username;

/**
 * @ORM\Column(name = "password", type="string", length=255)
 */
private $password;

/**
 * @ORM\Column(name="email", type="string", length=255, unique=true)
 */
private $email;

/**
 * @ORM\Column(type="datetime")
 */
private $registeredOn;

/**
 * @ORM\Column(type="integer", nullable=true)
 */
private $referrer;

/**
 * @ORM\Column(type="smallint")
 */
private $entries;

/**
 * @ORM\Column(type="string", length=3)
 */
private $currency;


 /** @see \Serializable::serialize() */
public function serialize()
{
    return serialize(array(
        $this->registeredOn,
        $this->id,
        $this->email,
        $this->username,
        $this->password,
        $this->roles,
        $this->referrer,
        $this->currency,
        $this->entries,
        $this->salt));
}

public function unserialize($serialized)
{
    list (
        $this->id,
        $this->email,
        $this->username,
        $this->password,
        $this->roles,
        $this->referrer,
        $this->currency,
        $this->entries,
        $this->salt) = unserialize($serialized, array('allowed_classes' => false));
}
public function eraseCredentials()
{

}
public function getRoles()
{
    return array("ROLE_USER");
}


public function getSalt()
{
    return $this->salt;
}


public function getId()
{
    return $this->id;
}

public function getUsername(): ?string
{
    return $this->username;
}

public function setUsername(string $username): self
{
    $this->username = $username;

    return $this;
}

public function getPassword(): ?string
{
    return $this->password;
}

public function setPassword(string $password): self
{
    $this->password = $password;

    return $this;
}

public function getEmail(): ?string
{
    return $this->email;
}

public function setEmail(string $email): self
{
    $this->email = $email;

    return $this;
}

public function getRegisteredOn(): ?\DateTimeInterface
{
    return $this->registeredOn;
}

public function setRegisteredOn(\DateTimeInterface $registeredOn): self
{
    $this->registeredOn = $registeredOn;

    return $this;
}

public function getReferrer(): ?int
{
    return $this->referrer;
}

public function setReferrer(?int $referrer): self
{
    $this->referrer = $referrer;

    return $this;
}

public function getEntries(): ?smallint
{
    return $this->entries;
}

public function setEntries($entries): self
{
    $this->entries = $entries;

    return $this;
}

public function setCurrency($currency): self
{
    $this->currency = $currency;

    return $this;
}
public function getCurrency(): ?string
{
    return $this->currency;
}

}

Security.yaml

security:
hide_user_not_found: false
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
encoders:
    App\Entity\User: sha256
providers:
    in_memory: { memory: ~ }
    main_db_provider:
        entity:
            class: App\Entity\User
            property: username

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        # anonymous: true
        pattern: ^/ #test
        anonymous: ~
        form_login:
            login_path: login
            check_path: login
            csrf_token_generator: security.csrf.token_manager
            provider: main_db_provider

        # activate different ways to authenticate

        # http_basic: true
        # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

        # form_login: true
        # https://symfony.com/doc/current/security/form_login_setup.html

# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
    - { path: ^/$, roles: ROLE_USER }
    # - { path: ^/$, roles: ROLE_USER }

1 个答案:

答案 0 :(得分:1)

序列化功能与反序列化功能不匹配!您需要更改它:

/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
    $this->registeredOn,
    $this->id,
    $this->email,
    $this->username,
    $this->password,
    $this->roles,
    $this->referrer,
    $this->currency,
    $this->entries,
    $this->salt));
}
public function unserialize($serialized)
{
   list (
    $this->registeredOn, # <--- look here it is missing
    $this->id,
    $this->email,
    $this->username,
    $this->password,
    $this->roles,
    $this->referrer,
    $this->currency,
    $this->entries,
    $this->salt) = unserialize($serialized, array('allowed_classes' => false));
}