Usage

1.x version
Maintained

Usage

Add these two lines in your layout:

  • Twig
    1
    2
    <script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
    <script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
    
  • PHP
    1
    2
    <script src="<?php echo $view['assets']->getUrl('bundles/fosjsrouting/js/router.js') ?>"></script>
    <script src="<?php echo $view['router']->generate('fos_js_routing_js', array('callback' => 'fos.Router.setData')) ?>"></script>
    

Note

If you are not using Twig, then it is no problem. What you need is to add the two JavaScript files above loaded at some point in your web page.

Generating URIs

It's as simple as calling:

1
Routing.generate('route_name', /* your params */)

Or if you want to generate absolute URLs:

1
Routing.generate('route_name', /* your params */, true)

Assuming some route definitions:

  • Annotations
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    // src/AppBundle/Controller/DefaultController.php
    
    /**
     * @Route("/foo/{id}/bar", options={"expose"=true}, name="my_route_to_expose")
     */
    public function indexAction($foo) {
        // ...
    }
    
    /**
     * @Route("/blog/{page}",
     *     defaults = { "page" = 1 },
     *     options = { "expose" = true },
     *     name = "my_route_to_expose_with_defaults",
     * )
     */
    public function blogAction($page) {
        // ...
    }
    
  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    # app/config/routing.yml
    my_route_to_expose:
        pattern: /foo/{id}/bar
        defaults: { _controller: AppBundle:Default:index }
        options:
            expose: true
    
    my_route_to_expose_with_defaults:
        pattern: /blog/{page}
        defaults: { _controller: AppBundle:Default:blog, page: 1 }
        options:
            expose: true
    

You can use the generate() method that way:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
Routing.generate('my_route_to_expose', { id: 10 });
// will result in /foo/10/bar

Routing.generate('my_route_to_expose', { id: 10, foo: "bar" });
// will result in /foo/10/bar?foo=bar

$.get(Routing.generate('my_route_to_expose', { id: 10, foo: "bar" }));
// will call /foo/10/bar?foo=bar

Routing.generate('my_route_to_expose_with_defaults');
// will result in /blog/1

Routing.generate('my_route_to_expose_with_defaults', { id: 2 });
// will result in /blog/2

Routing.generate('my_route_to_expose_with_defaults', { foo: "bar" });
// will result in /blog/1?foo=bar

Routing.generate('my_route_to_expose_with_defaults', { id: 2, foo: "bar" });
// will result in /blog/2?foo=bar

Moreover, you can configure a list of routes to expose in app/config/config.yml:

1
2
3
# app/config/config.yml
fos_js_routing:
    routes_to_expose: [ route_1, route_2, ... ]

These routes will be added to the exposed routes. You can use regular expression patterns if you don't want to list all your routes name by name.

You can prevent to expose a route by configuring it as below:

1
2
3
4
5
6
# app/config/routing.yml
my_very_secret_route:
    pattern: /admin
    defaults: { _controller: HelloBundle:Admin:index }
    options:
        expose: false

HTTP Caching

You can enable HTTP caching as below:

1
2
3
4
5
6
7
8
9
# app/config/config.yml
fos_js_routing:
    cache_control:
        # All are optional, defaults shown
        public: false   # can be true (public) or false (private)
        maxage: null    # integer value, e.g. 300
        smaxage: null   # integer value, e.g. 300
        expires: null   # anything that can be fed to "new \DateTime($expires)", e.g. "5 minutes"
        vary: []        # string or array, e.g. "Cookie" or [ Cookie, Accept ]

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