我们有一个Symfony2项目,由于某些原因,我们已经更改了主机,在使用Plesk Panel更改为主机后,我们面临The CSRF token is invalid. Please try to resubmit the form.
,我试图找出原因?然后提供一个简单的Twig表单,其中包含以下代码:
<!doctype html>
<html>
<head>
{# ... #}
<link rel="stylesheet"
href="{{ asset('assets/vendor/bootstrap/dist/css/bootstrap.min.css') }}">
</head>
<body>
<div class="container">
<h2>ADD Item to Crawling List</h2>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
</div>
</body>
</html>
并正确呈现
<!doctype html>
<html>
<head>
<link rel="stylesheet"
href="/assets/vendor/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>ADD Item to Crawling List</h2>
<form name="form" method="post">
<div id="form"><div><label for="form_post_id" class="required">Instagram Post Id</label><input type="text" id="form_post_id" name="form[post_id]" required="required" /></div><div><button type="submit" id="form_save" name="form[save]">Fetch Comments</button></div><input type="hidden" id="form__token" name="form[_token]" value="j9YWKseFdkU4Z_Yc5nuwm72SKD4dT06gJpARLqPID6I" /><button type="submit" id="form_save" name="form[save]">Fetch Comments</button></div>
</form>
</div>
</body>
</html>
还尝试添加{{ form_rest(form) }}
但不是任何成就,结果是相同的。
另一个注意事项是我们没有覆盖config
中的会话配置处理程序.yml`及其
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
任何有助于我们解决此问题的解决方案或想法都将受到赞赏。
答案 0 :(得分:2)
你应该添加:
{{ form_rest(form) }}
在form_end之前,这会在表单中的隐藏输入中添加CSRF令牌。