简体   繁体   English

如何使用Javascript中的参数值生成Symfony 2 twig URL?

[英]How to generate Symfony 2 twig URL with parameter value from Javascript?

I want to append query string parameters from javascript (jquery) to symfony 2 url. 我想将查询字符串参数从javascript(jquery)附加到symfony 2 url。 I want to pass the value of selected radio button to ajax request egin plain php I would have done like 我想将选定的单选按钮的值传递给ajax请求,例如我想做的普通php

$(document).ready(function() {
    $('.id_radio').click(function() {                      
        $.ajax({
            'url': 'example.php?id='+($this).val(),
            'success': function(r) {
                $('#div1').html(r);
            }
        });
    });

How to generate such URL in symfony2 twig ? 如何在symfony2 twig中生成这样的URL? We generate URL in symfony 2 twig like 我们在symfony 2 twig中生成URL

{{ path('example_path', {'id': id}) }} where id is twig variable. {{ path('example_path', {'id': id}) }}其中id是twig变量。

I tried using FOSJsRoutingBundle but don't know if it is for same problem and not sure how to use it ? 我尝试使用FOSJsRoutingBundle,但不知道是否是同样的问题,不知道如何使用它?

I tried below but it is not working. 我在下面试过,但它没有用。

$('.id_radio').click(function() {
    alert(Routing.generate('example_path', {
        'id': $(this).val()
        }));
    $.ajax({
        'url': Routing.generate('example_path', {
            'id': $(this).val()
            }),
        'success': function(r) {
            $('#div1').html(r);
        }
    });
});

Solution mentioned below by koskoz worked, I did not pass options={"expose"=true} but after adding it worked. koskoz下面提到的解决方案工作,我没有传递options = {“expose”= true}但是在添加它之后工作了。

But there is one more problem. 但还有一个问题。 It works only until we do not refresh page. 它只在我们不刷新页面之前有效。 If we refresh page, it does not work. 如果我们刷新页面,它不起作用。 To make it work, I have to delete symfony cache files. 为了使它工作,我必须删除symfony缓存文件。

It does not work in other browser if we have already opened it in one browser. 如果我们已经在一个浏览器中打开它,它在其他浏览器中不起作用。

This is exactly what FOSJsRoutingBundle is aiming for: 这正是FOSJsRoutingBundle的目标:

You configure the route you want to expose in JavaScript, load the JS and then simply do something like this : 您可以在JavaScript中配置要公开的路由,加载JS然后只需执行以下操作:

Routing.generate('my_route_to_expose', { id: 10 });

Instead of 代替

/**
 * @Route("/example/{name}", name="example_path", options={"expose"=true})
 * @Template
 */
public function exampleAction($name)
{

}

and using FOSJsRoutingBundle 并使用FOSJsRoutingBundle

$('.id_radio').click(function() {
    alert(Routing.generate('example_path', {'id': $(this).val()}));
    $.ajax({
        'url': Routing.generate('example_path', {'id': $(this).val()}),
        'success': function(r) {
            $('#div1').html(r);
        }
    });
});

Below worked better for me, 以下对我来说效果更好,

     /**
     * @Route("/example", name="example_path", options={"expose"=true})
     * @Method({"GET"})
     * @Template 
     */
    public function exampleAction()
    {


    }

$('.id_radio').click(function() {
    $.ajax({
        'url': '{{ path('example_path') }}',
        'data': 'name='+$(this).val(),
        'type': 'GET',
        'success': function(r) {
            $('#div1').html(r);
        }
    });
});

Get the fosjsrouting bundle from here: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle and in your routing file add for that route 从这里获取fosjsrouting包: https//github.com/FriendsOfSymfony/FOSJsRoutingBundle并在您的路由文件中为该路由添加

options: expose: true options:expose:true

And in your js file add url = Routing.generate('route_name', {parameter: parameter_value}); 在你的js文件中添加url = Routing.generate('route_name',{parameter:parameter_value});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM