Routing for ajax in symfony project -


first. working, tips projekt.

i create many projects symfony2 , there everywhere use ajax, query.

here routing.yml

_ajax:     path: /_ajax     defaults: { _controller: acmeallgemeinbundle:default:ajax } 

here call in 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 question:

should handle evey ajax 1 route , set 1 variable , call functions name in controller like

{"job":"saveadress"} 

or should create different route's every job like

_ajax_saveadress:     path: /_ajax/saveadress     defaults: { _controller: acmeallgmeinbundle:default:ajaxsavejob } 

you may find easier in larger application split out paths individual routes , base routes logically on domain objects and/or operations.

i recommend considering restful approach urls operations might following:

  • get /jobs/{jobid}/address
  • post /jobs/{jobid}/address (also accepting put , delete methods)

if want fancy can leverage fosrestbundle along serializer (e.g. jmsserializerbundler) handle formatting of requests in either html, json, or xml necessary.


Comments

Popular posts from this blog

visual studio - vb.net filter binding source by time -

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -