-1
我正在嘗試使用Symfony創建Web API,但POST方法不起作用。我的代碼不會給出任何錯誤。Symfony 3.1發佈到Web Api
實體/狀態:
<?php
namespace RestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
*
*
* @ORM\Table(name="status")
* @ORM\Entity
*/
class Status
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nome", type="string", length=100, nullable=true)
*
* @Assert\NotBlank()
*/
private $nome;
}
?>
POST功能(控制器/ StatusController):
public function postStatusAction(Request $request)
{
$user = new Status();
$form = $this->createForm(StatusType::class, $user);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $user;
}
//var_dump($form->getErrors());
throw new HttpException(400, "Invalid data");
}
窗體/ StatusType:
<?php
/**
* Created by PhpStorm.
* Date: 10/08/16
* Time: 10:39
*/
namespace RestBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class StatusType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nome', TextType::class);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'RestBundle\Entity\Status',
'csrf_protection' => false,
));
}
/**
* @return string
*/
public function getBlockPrefix()
{
return '';
}
}
感謝。
仍然沒有工作https://postimg.org/image/5z3fsel4b/ – user26776