English spoken conference

Symfony 5: The Fast Track

A new book to learn about developing modern Symfony 5 applications.

Support this project

WARNING: You are browsing the documentation for Symfony 4.0 which is not maintained anymore. Consider upgrading your projects to Symfony 4.3.

You are browsing the Symfony 4.0 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4.0 yet, browse the Symfony 3.4 documentation.

How to Generate Routing URLs in JavaScript

4.0 version
Maintained Unmaintained

How to Generate Routing URLs in JavaScriptΒΆ

If you're in a Twig template, you can use the same path() function to set JavaScript variables. The escape() function helps escape any non-JavaScript-safe values:

1
2
3
<script>
var route = "{{ path('blog_show', {'slug': 'my-blog-post'})|escape('js') }}";
</script>

But if you actually need to generate routes in pure JavaScript, consider using the FOSJsRoutingBundle. It makes the following possible:

1
2
3
4
5
<script>
var url = Routing.generate('blog_show', {
    'slug': 'my-blog-post'
});
</script>

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.