M G
M G

Reputation: 1250

Path to bundle from AJAX

Basically I have a bundle in Symfony with routing.yml:

main_move:
    pattern:  /move/{direction}
    defaults: { _controller: MainBundle:Move:move }

And javascript file

$(".left_button").click(function(){
    $.ajax({
    url: '/move/1'
    }).done(function(){
    $(".button").css("width","1000px");
    });
});

Can I only put full path to that bundle or it can be like in twig: {{ path(main_move)}}(I have already tried that one)

Upvotes: 0

Views: 69

Answers (2)

M G
M G

Reputation: 1250

Ok I did it like:

$(".move_button").click(function(){
    var path = $(this).attr("data-path");
    $.ajax({
    url: path
    }).done(function(){
    $(".button").hide();
    });
});

And in twig:

data-path="{{ path('main_move', { 'direction': 1 }) }}"

Upvotes: 1

Pi Wi
Pi Wi

Reputation: 1085

Use JSRoutingBundle for this: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

Upvotes: 2

Related Questions