0

First. All is working, but i your tips for the projekt.

I create many projects with Symfony2 and there everywhere i use ajax, mostly with query.

Here is my routing.yml

_ajax:
    path: /_ajax
    defaults: { _controller: AcmeAllgemeinBundle:Default:ajax }

Here the call in the js

function checkStatus()
{
    var url = $("#ajaxurl").data("url");

    var postData = [
        { "id":"1", "name":"bob"},
        { "id":"2", "name":"jonas"}
    ]

    $.ajax({
            type: "POST",
            data: postData,
            url: url,
            dataType: "json"
        }).done( function(resp) {
            console.log("mit done " + resp.text);
        }).fail( function(){
            console.log("Fehler");
        });
}

But here is the question:

Should i handle evey ajax with one route and set one variable and call the functions by name in the controller like

{"job":"saveAdress"}

or should I create different route's for every job like

_ajax_saveAdress:
    path: /_ajax/saveAdress
    defaults: { _controller: AcmeAllgmeinBundle:Default:ajaxSaveJob }
1
  • Its based on the requirements what you want to achieve from ajax a validation check, send email ,db usage etc Commented Mar 10, 2014 at 20:06

1 Answer 1

2

You may find it easier in a larger application to split out your paths into individual routes and base the routes logically on their domain objects and/or operations.

I would recommend considering a RESTful approach to your URLs so that you operations might look something like the following:

  • GET /jobs/{jobId}/address
  • POST /jobs/{jobId}/address (also accepting PUT and DELETE methods)

If you want to get fancy you can leverage the FOSRestBundle along with a serializer (e.g. JMSSerializerBundler) to handle formatting of your requests in either HTML, JSON, or XML as necessary.

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

Comments

Your Answer

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