FriendsOfSymfony/FOSJsRoutingBundle — Issue #17: Consider an addDefaults/addDefaultParams method in router.js
Author: jonathaningram
Created: 2011-12-02 04:27:30+00:00
State: open

Imagine you have some sort of URI setup that looks like this: `/{companySlug}/{projectSlug}` and you might have a route in the project that is `my_bundle_users` that looks like `/{companySlug}/{projectSlug}/users`.

Now, when generating a route using `Routing.generate('my_bundle_users')`, you must specify the `companySlug` and the `projectSlug` each time. However, (and in my case), you might have a twig layout for the company bundle (that extends a core layout) and a twig layout for the project bundle (that extends the company layout). If you could set default route params onto the router somehow, you'd avoid having to pass repetitive params to the router.

Your company bundle layout might look like this:

```jinja
{% extends 'JbiCoreBundle::layout.html.twig' %}

{% set company = app.request.get('company') %}

{# in some block do this: #}
Routing.addDefaultParam({'companySlug': {{ company.slug }}});
```

Your project bundle layout might look like this:

```jinja
{% extends 'JbiCompanyBundle::layout.html.twig' %}

{% set project = app.request.get('project') %}

{# in some block do this: #}
Routing.addDefaultParam({'projectSlug': {{ project.slug }}});
```

Then you can do this:

```js
Routing.generate('my_bundle_users')
```

And the company and project slug are picked up automatically.