简体   繁体   中英

How to link JS route var and Symfony 2 URLs

I would like to know what is the most flexible solution to link SF2 routes & a JS var which is containing the routes. For static routes, I don't have any problems, but when I want to connect URL with parameters, i did not find any solution (not hard-coded).

How can I do if I edit the routing.yml file, Dynamics URL on change

I'm using var url = "domain.com/path/to/url/with/" + token + "/and/" + name for generating but it is not flexible.

I know that twig generated urls are server side etc...

Any solutions ?

Ex :

JS

var route = {
    home : "{{ path("home") }}",
    custom_route : "{{ path("my_custom_route") }}"
}

Routing.yml

my_custom_route:
    pattern:  /path/to/url/with/{token}/and/{name}
    defaults: { _controller: "SupboxCloudBundle:Public:file" }

我认为您可以尝试FOSJSRoutingBundle

Well, this sort of depends on the result you are trying to achieve. Are you doing this in some sort of JS app?

If it were me, I would render the routes, or route parameters in the DOM. I wouldn't mess with rendering anything in the JS and I would make the JS code abstract.

For instance:

<div data-custom="{{ path("my_custom_route") }}"></div>
<div data-home="{{ path('home') }}"

or

<a href="{{ path("my_custom_route") }}" data-route="home">whatever</a>

then you can grab your routes in JS when needed (from the first example):

var route = {
    home: $('div[data-home]').data('home'),
    custom_route: $('div[data-custom]').data('custom')
}

This is a little messy as I'm just trying to show you a concept. However your actual implementation would depend on the actual result you are trying to achieve.

If you comment a more specific intent below I will provide a better solution for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM