我想知道如何使用Symfony2中的(html链接标记)将数据从twig传递到控制器。我们假设我们有一个名为“test.html.twig”的twig文件,其代码如下:
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<script>
var a = "hello";
var b = "hi";
var c = $('<a href="">send data</a>');
$('body').append(c);
</script>
</body>
如何通过html链接标记a
将两个变量b
和<a>
的值传递给Symfony2中的控制器?
答案 0 :(得分:2)
您可以将变量作为参数发送到控制器,如下所示
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<script>
var a = "hello";
var b = "hi";
var c = $('<a href="/hello/' + a + '/'+ b +'">send data</a>');
$('body').append(c);
</script>
</body>
random_route:
path: /hello/{a}/{b}
defaults: { _controller: SomeBundle:Controller:Action }
public function randomactionAction($a, $b) {
// $a = 'hello', $b = 'hi'
}