Symfony, fos_js_routing and problems with “_locale”

this is my app/routing.yml


resource: "@FrontendBundle/Resources/config/routing.yml" prefix: /{_locale}/ requirements: _locale: en|es


resource: "@BackendBundle/Resources/config/routing.yml" prefix: /{_locale}/app requirements: _locale: en|es

fos_js_routing: resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

and this is my BackendBundle\Resources\Config\routing.yml


pattern: /getModelsFromMake/{idMake} defaults: { _controller: BackendBundle:Backend:getModelFromMake } options: expose: true

and my problems is when i do a ajax call like this:

$.ajax({ type: "POST",

url: Routing.generate('getModelsFromMake'), data: { idMake: $('#make').val(), }, dataType: "json", success: function(data) { console.log("All OK"); }, error: function() { console.log("ERROR"); } });

the chrome inspector tell me:

Uncaught Error: The route "getModelsFromMake" requires the parameter "_locale".

any idea to solve this?


in my layout I have this:

<script type="text/javascript" src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script> <script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>


if I run the command "php app/console fos:js-routing:debug" I get this:

Name Method Pattern getModelsFromMake ANY /{_locale}/app/getModelsFromMake


Try to insert this snippet of code before your ajax

<script> var locale= {{ app.request.locale }} ; </script>

and modify your script as follows

$.ajax({ type: "POST", url: Routing.generate('getModelsFromMake', array('_locale' => locale)), data: { idMake: $('#make').val(), }, dataType: "json", success: function(data) { console.log("All OK"); }, error: function() { console.log("ERROR"); } });


To set the request locale to all your call to Routing.generate, you can override this method and add the {{app.request.locale}} param in every call. The following script has to be executed in a twig template and after including routing script.

<!-- Include JSRouting libs & exposed routes --> <script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script> <script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script> <script> $(function () { // change name of initial method Routing.generateImpl = Routing.generate; // override generate fonction by adding a default _locale from request locale Routing.generate = function (url, params) { var paramsExt = {}; if (params) { paramsExt = params; } if (!paramsExt._locale){ paramsExt._locale = '{{ app.request.locale }}'; } return Routing.generateImpl(url, paramsExt); } }) </script>

Now you can use Routing.generate transparently without worry about the _locale param !

// in any JS file var url = Routing.generate('mypath'); var url2 = Routing.generate('another path', {param: 1});

To clarify the code, it could be possible to isolate the previous script in a js file. For that a global variable has to be defined as told by @DonCallisto .

<!-- for exemple in the <head> of the page --> <script> var REQUEST_LOCALE = '{{ app.request.locale }}'; </script>

And so in the script you can use REQUEST_LOCALE

// this line paramsExt._locale = '{{ app.request.locale }}'; // has to be replaced by paramsExt._locale = REQUEST_LOCALE;


  • Reading contents of a managed bean with reflection in a JSF application
  • KoGrid JSON Dynamic widgets, with nested server calls
  • Cleave.js Phone CA
  • Storing data from SQL in array
  • jQuery file download plugin
  • Understanding Intl.DateTimeFormat as a JavaScript object
  • How to load Q library with Require.js?
  • Rails 5 - Google Maps - Javascript error - initMap is not a function - fixing one js issue creates a
  • Group list of tuples by item
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Is playing sound in Javascript performance heavy?
  • Chrome doesn't support silverlight anymore? How to solve this?
  • jQuery .attr() and value
  • Using $this when not in object context
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • Array.prototype.includes - not transformed with babel
  • Volley JsonObjectRequest send headers in GET Request
  • Accessing IRQ description array within a module and displaying action names
  • Is there a amazon webstore API for customers?
  • How to get a value (ex: baseURL) in every Karate feature?
  • Perl system calls when running as another user using sudo
  • Javascript convert timezone issue
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • Build own AppleScript numerical error handling
  • Websockets service method fails during R startup
  • Google cloud sdk not working when python points python3
  • How to pass list parameters for each object using Spring MVC?
  • Is there a mandatory requirement to switch app.yaml?
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app