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

查看:259
本文介绍了使用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中的元素(下面的代码)?

The question is: how can i generate this route based on a select element in javascript (code below)?

问题是:我无法使用路径 url 来自Symfony的助手,因为他们需要指定 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天全站免登陆