我正在使用symfony REST API,目前我因应PUT请求停止编辑数据。我不知道在哪里搜索错误。
我的代码如下。
Controller.php
/**
* User update
*
* @Rest\Put("/users/{id}", name="user_update")
* @Rest\View()
* @param Request $request
* @param $id
* @param Users $restUser
*
*
*
* @return View
*/
public function updateUserAction( Request $request, $id, Users $restUser )
{
$results = [];
$user = $this->em->getRepository( User::class )
->find( $id )
;
/**
* Data to update user
*/
if ( ! $user )
{
$results[ 'message' ] = sprintf( 'No users found with {%s} ID', $id );
$results[ 'status' ] = 'failure';
return View::create( $results );
}
$results[ 'beforeUpdate' ] = $user;
$newData = $request->request->all();
/**
* Try to update
*/
try
{
$form = $this->createForm( UserType::class, $user );
$form->submit( $newData );
if ( $form->isValid() )
{
$results[ 'status' ] = 'success';
$results[ 'message' ] = 'Pomyślnie zaaktualizowsano użytkownika';
$this->em->persist( $user );
$this->em->flush();
}
$results[ 'errors' ] = $form->getErrors( FALSE, TRUE );
}
catch( \Exception $exception )
{
$results[ 'status' ] = 'failure';
$results[ 'message' ] = 'Wystąpił błąd przy aktualizacji';
$results[ 'message_exception' ] = $exception->getMessage();
}
/**
* Get BODY of request
*/
$results[ 'sentData' ] = $newData;
return View::create($results);
}
和UserFormType.php
class UserType extends AbstractType implements FormTypeInterface{
public function configureOptions( OptionsResolver $resolver )
{
$resolver->setDefaults( [
'data_class' => User::class,
'allow_extra_fields' => TRUE,
'csrf_protection' => FALSE,
] );
}
}
基本问题是我一切正常吗?我是Symfony的新手,每次尝试做一些我认为得到每个可用错误的事情时,我都会尽力而为。