0

I have a Symfony app using a little bit of Javascript. There is a ajax request calling an API in Symfony:

$.ajax({
    type: "POST",
    url: "http://symfony.dev/api/price",
    cache: false,
    data: {
        'volume': $('#tunnel_step_one_volume').val(),
        'delivery': $('#tunnel_step_one_isStandardDelivery').val()
    },

    success: function (data, status) {
        console.log(data);
        Command.refreshData(data);
    },

    error: function (jqXHR, textStatus, errorThrown) {
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
    }
})

Now the URL is hard coded.

My question is:

What is the best practices to give the JS file my route name in Symfony?

2
  • 1
    if you use twig, you can pass a variable to the template and use the variable in the js included. Commented May 2, 2016 at 11:54
  • check this option: stackoverflow.com/a/2190927/2209876 Commented May 2, 2016 at 11:55

1 Answer 1

1

You can use https://github.com/FriendsOfSymfony/FOSJsRoutingBundle to achieve this.

The easiest way would be to return the route from the controller and print in a js variable inside your page:

<script> myUrl = {{myUrl}} </script>

But i wouldn't consider it best practice.

Sign up to request clarification or add additional context in comments.

1 Comment

Your easy way is a bad practice. The bundle is a nice practice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.