【问题标题】:How to pass data from twig to controller using <a> (html link tag) in Symfony2?如何在 Symfony2 中使用 <a>(html 链接标签)将数据从树枝传递到控制器?
【发布时间】:2014-07-08 09:13:23
【问题描述】:

我想知道如何在 Symfony2 中使用(html 链接标签)将数据从树枝传递到控制器。假设我们有一个名为“test.html.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 链接标签 &lt;a&gt; 将两个变量 ab 的值传递给 Symfony2 中的控制器?

【问题讨论】:

    标签: symfony hyperlink controller twig


    【解决方案1】:

    您可以将变量作为参数发送到您的控制器,如下所示

    <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'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      相关资源
      最近更新 更多