如何使用<a> (html link tag) in Symfony2?</a>将数据从twig传递到控制器

时间:2014-07-08 09:13:24

标签: symfony hyperlink controller twig

我想知道如何使用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中的控制器?

1 个答案:

答案 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>

The Routing

random_route:
  path: /hello/{a}/{b}
  defaults: { _controller: SomeBundle:Controller:Action }

The controller

public function randomactionAction($a, $b) {
  // $a = 'hello', $b = 'hi'
}