使用 Twig 和 Symfony2 在 javascript 中生成路由 [英] Generating routes in javascript with Twig and Symfony2
问题描述
很奇怪的问题,对不起,我对 Symfony/Twig 很陌生.我的路线需要一个强制性的 region_id
参数:
Quite odd problem, sorry for asking, i'm quite new to Symfony/Twig. My route requires a mandatory region_id
paramenter:
ajax_provinces_by_region:
pattern: /ajax/region/{region_id}/provinces
defaults: {_controller: SWAItaliaInCifreBundle:Ajax:provincesByRegion }
requirements: {region_in: d+}
问题是:我如何根据javascript中的select
元素生成这条路线(下面的代码)?
The question is: how can i generate this route based on a select
element in javascript (code below)?
问题是:我不能使用来自 Symfony 的 path
和 url
助手,因为它们需要指定 region_id
参数 (this.value
) 我无法访问,因为它是一个 javascript 变量(并且 Twig 在服务器端编译).
The problem is: i can't use path
and url
helpers from Symfony as they require to specify the region_id
parameter (this.value
) i can't access because it's a javascript variable (and Twig is compiled server-side).
$(document).ready(function() {
$('select#regions').change(function(){
// Make an ajax call to get all region provinces
$.ajax({
url: // Generate the route using Twig helper
});
});
});
推荐答案
您可以使用 FOSJsRoutingBundle.
这篇关于使用 Twig 和 Symfony2 在 javascript 中生成路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!