Php 邮差SYMFONY错误
当我尝试在/用户处发布以下内容时,出现此错误: { “用户名”:“Ausername123”, “密码”:“Apassword123”, “重新键入密码”:“Apassword123” } 类型错误:传递给Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::encodePassword()的参数1必须是Symfony\Component\Security\Core\User\UserInterface的实例,AppBundle\Entity\User给定的实例,在第160行的/home/rluis/Stage\u Tek2/Boojon/src/AppBundle/Controller/userscocontroller.php中调用 我的用户控制器:Php 邮差SYMFONY错误,php,sql,symfony,postman,Php,Sql,Symfony,Postman,当我尝试在/用户处发布以下内容时,出现此错误: { “用户名”:“Ausername123”, “密码”:“Apassword123”, “重新键入密码”:“Apassword123” } 类型错误:传递给Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::encodePassword()的参数1必须是Symfony\Component\Security\Core\User\UserInterface的实例,AppBundle
<?php
/**
* Created by PhpStorm.
* User: rluis
* Date: 7/11/18
* Time: 2:08 PM
*/
namespace AppBundle\Controller;
use AppBundle\Entity\EntityMerger;
use AppBundle\Entity\User;
use AppBundle\Exception\ResourceValidationException;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use FOS\RestBundle\Controller\Annotations as Rest;
/**
* @Security("is_anonymous() or is_authenticated()")
*/
class UsersController extends AbstractController
{
/**
* @var UserPasswordEncoderInterface
*/
private $passwordEncoder;
/**
* @var JWTEncoderInterface
*/
private $jwtEncoder;
/**
* @var EntityMerger
*/
private $entityMerger;
/**
* UsersController constructor.
* @param UserPasswordEncoderInterface $passwordEncoder
* @param JWTEncoderInterface $jwtEncoder
* @param EntityMerger $entityMerger
*/
public function __construct(UserPasswordEncoderInterface $passwordEncoder, JWTEncoderInterface $jwtEncoder, EntityMerger $entityMerger)
{
$this->passwordEncoder = $passwordEncoder;
$this->jwtEncoder = $jwtEncoder;
$this->entityMerger = $entityMerger;
}
/**
* @Rest\View(serializerGroups={"user_detail"})
* @Security("is_granted('show', theUser)", message="Access denied")
*/
public function getUserAction(?User $theUser)
{
if (null === $theUser) {
throw new NotFoundHttpException();
}
return $theUser;
}
/**
*
* @Rest\Post(
* path = "/users",
* name = "users_add"
* )
* @Rest\View(StatusCode=201, serializerGroups={"user_detail"})
* @ParamConverter(
* "user",
* converter="fos_rest.request_body",
* options={"deserializationContent"={"groups"={"Deserialize"}}}
* )
*/
public function postUserAction(User $user, ConstraintViolationListInterface $violations)
{
if (count($violations) > 0) {
$message = 'The user is not valid: ';
foreach ($violations as $violation) {
$message .= sprintf(
"Field %s: %s ",
$violation->getPropertyPath(),
$violation->getMessage()
);
}
throw new ResourceValidationException($message);
}
$this->encodePassword($user);
$user->setRoles([User::ROLE_USER]);
$this->persistUser($user);
return $user;
}
/**
* @Rest\View(serializerGroups={"user_detail"})
* @Rest\Patch(
* path = "/users/{theUser}",
* name= "patch_user"
* )
* @ParamConverter(
* "modifiedUser",
* converter="fos_rest.request_body",
* options={
* "validator"={"groups"={"Patch"}},
* "deserializationContext"={"groups"={"Deserialize"}}
* }
* )
* @Security("is_granted('edit', theUser)", message="Access Denied")
*/
public function patchUserAction(?User $theUser, User $modifiedUser, ConstraintViolationListInterface $violations)
{
if (null === $theUser) {
throw new NotFoundHttpException();
}
if (empty($modifiedUser->getPassword())) {
$modifiedUser->setPassword(null);
}
if (count($violations) > 0) {
$message = 'The user is not valid: ';
foreach ($violations as $violation) {
$message .= sprintf(
"Field %s: %s",
$violation->getPropertyPath(),
$violation->getMessage()
);
}
throw new ResourceValidationException($message);
}
$this->entityMerger->merge($theUser, $modifiedUser);
$this->encodePassword($theUser);
$this->persistUser($theUser);
return $theUser;
}
/**
* @param User $user
*/
protected function encodePassword(User $user)
{
$user->setPassword(
$this->passwordEncoder->encodePassword(
$user,
$user->getPassword()
)
);
}
/**
* @param User $user
*/
protected function persistUser(User $user)
{
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
}
您的用户
实体必须实现Symfony\Component\Security\Core\User\UserInterface
以及该接口中定义的所有必要方法:
use Symfony\Component\Security\Core\User\UserInterface;
class User implements UserInterface {
/* implement missing methods */
}
use Symfony\Component\Security\Core\User\UserInterface;
class User implements UserInterface {
/* implement missing methods */
}