我正在尝试实现FOSRestBundle和Symfony表单。我找到了这个教程,但我对这部件有问题
private function processForm(PageInterface $page, array $parameters, $method = "PUT")
{
$form = $this->formFactory->create(new PageType(), $page, array('method' => $method));
$form->submit($parameters, 'PATCH' !== $method);
if ($form->isValid()) { //form is not valid.
$page = $form->getData();
$this->om->persist($page);
$this->om->flush($page);
return $page;
}
throw new InvalidFormException('Invalid submitted data', $form);
}错误: CSRF令牌无效。请尽量重新提交表格。
这里是教程中的控制器。这是我的班长:
public function newAction(Request $request)
{
$form = new EntryType();
$newEntry = $this->container->get('entries.entry.handler')->post(
$request->request->get($form->getName())
);
return View::create()
->setStatusCode(200)
->setFormat('json')
->setSerializationContext(SerializationContext::create()->setGroups(array('list')))
->setData($newEntry);
}我应该跳过检查isValid()还是以某种方式修复这个问题?多么?
好了,现在很清楚了。应禁用CRF验证(csrf_protection)。
从php客户端调用rest post api时CSRF令牌无效 https://github.com/liuggio/symfony2-rest-api-the-best-2013-way/issues/1#issuecomment-31435232 在使用RESTful API时是否需要CSRF验证?
发布于 2016-04-05 08:47:30
来自教程第3部分:
可以根据用户的角色禁用CSRF。
# app/config/config.yml
fos_rest:
disable_csrf_role: ROLE_API
# you can also try
# disable_csrf_role: IS_AUTHENTICATED_FULLY也请参阅这个问题。
https://stackoverflow.com/questions/36420516
复制相似问题