问题
this is my app/routing.yml
FrontendBundle:
resource: "@FrontendBundle/Resources/config/routing.yml" prefix: /{_locale}/ requirements: _locale: en|es
BackendBundle:
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
getModelsFromMake:
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?
EDIT 1:
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>
EDIT 2:
if I run the command "php app/console fos:js-routing:debug" I get this:
Name Method Pattern
getModelsFromMake ANY /{_locale}/app/getModelsFromMake
回答1:
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"); }
});
回答2:
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;
来源:https://stackoverflow.com/questions/25842418/symfony-fos-js-routing-and-problems-with-locale