使用 Twig 和 Symfony2 在 javascript 中生成路由 [英] Generating routes in javascript with Twig and Symfony2

查看:23
本文介绍了使用 Twig 和 Symfony2 在 javascript 中生成路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很奇怪的问题,对不起,我对 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 的 pathurl 助手,因为它们需要指定 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆