<p>You can use the <a href="https://craftcms.com/docs/plugins/hooks-reference#registerSiteRoutes" rel="nofollow"><code>registerSiteRoutes()</code></a> hook in your plugin's primary class.</p>

<p>Assuming your custom URL formats are stored in an array in a plugin setting called <code>customRoutes</code> (use <code>AttributeType::Mixed</code> for arrays), and that the array consists of key/value paired routes and template paths:</p>

<pre><code>public function registerSiteRoutes()
{
    return $this-&gt;getSettings()['customRoutes'] ?: array();
}
</code></pre>

<p>Here's how the <code>customRoutes</code> array should look:</p>

<pre><code>array(
    'some-custom-route' =&gt; '_some-custom-template',
    'another-custom-route' =&gt; '_another-custom-template',
),
</code></pre>

CC BY-SA 3.0
2016-08-18T15:10:12.657Z
If the route has brackets for tokens will Craft parse that or does it need to be converted somehow?
a-am
CC BY-SA 3.0
2016-08-18T15:28:25.023Z
Unlike URL formats for entries and categories, routes declared with `registerSiteRoutes()` use [sub patterns](https://craftcms.com/docs/routing#accessing-subpatterns-in-your-templates) for parameters and won't parse Twig or URL format tokens (i.e. `{slug}`). What kind of variables do you need in your routes?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-18T15:35:11.250Z
So entries and categories because they are elements use the element's `routeRequestForMatchedElement` method?  I was trying to give admins ability to set any variable like section urlFormat provides for entires.
a-am
CC BY-SA 3.0
2016-08-18T19:05:41.620Z
What's the context for those variables, though? E.g. `{slug}` in an Section's URL format is really just short for `{entry.slug}`, but unlike URL formats, a generic route doesn't have that kind of context. To give any further advice or suggestions, it'd be helpful to learn more about your actual use case – what kind of routes are your admins going to add, and what kind of variables are you wanting to expose to those routes?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-18T19:12:48.340Z
Admins will probably use slug, date('y-m-d') & id as most common types of variables.
a-am
CC BY-SA 3.0
2016-08-19T17:26:09.833Z
I think you have two options – either write some custom logic in `registerSiteRoutes` to rewrite whatever "bracket variables" the user has added to valid sub patterns, i.e. `{slug}` becomes `(?P<slug>[-\w]+)` (don't see how you can avoid having every sub pattern accept any character, as opposed to having different kinds of sub patterns as per [the official docs](https://craftcms.com/docs/routing#accessing-subpatterns-in-your-templates)). The other option is to have a set of pre-defined variables (i.e. _tokens_), that are replace with different kinds of sub patterns.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-19T17:27:17.577Z
The latter approach – pre-defined tokens representing various sub patterns – is how the routing UI inside the Craft CP works, so you might check out how P&T built it and try to reuse or recreate some of that functionality.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-18T06:33:33.307Z
2016-08-18T06:33:33.307Z
Mats Mikkel Rummelhoff
<p>In my plugin settings I am allowing the user to specify url formats for each locale along with desired template path, much like sections.  How do I then register those custom url formats as site routes?</p>

<plugin-development><routes><routing>
How to setup custom site routes created by user in plugin settings
# How to setup custom site routes created by user in plugin settings

(Asked by: username_0 on 2016-08-18)

In my plugin settings I am allowing the user to specify url formats for each locale along with desired template path, much like sections. How do I then register those custom url formats as site routes?

**Tags:** plugin-development, routes, routing


---

## 1 answers

### Answer by username_1 on 2016-08-18. Score: 5 | Accepted answer

You can use the registerSiteRoutes() hook in your plugin's primary class.
Assuming your custom URL formats are stored in an array in a plugin setting called customRoutes (use AttributeType::Mixed for arrays), and that the array consists of key/value paired routes and template paths:
public function registerSiteRoutes()
{
    return $this->getSettings()['customRoutes'] ?: array();
}

Here's how the customRoutes array should look:
array(
    'some-custom-route' => '_some-custom-template',
    'another-custom-route' => '_another-custom-template',
),

> **Comments:**
> **username_0**: If the route has brackets for tokens will Craft parse that or does it need to be converted somehow?  
> **username_1**: Unlike URL formats for entries and categories, routes declared with `registerSiteRoutes()` use [sub patterns](https://craftcms.com/docs/routing#accessing-subpatterns-in-your-templates) for parameters and won't parse Twig or URL format tokens (i.e. `{slug}`). What kind of variables do you need in your routes?  
> **username_0**: So entries and categories because they are elements use the element's `routeRequestForMatchedElement` method?  I was trying to give admins ability to set any variable like section urlFormat provides for entires.  
> **username_1**: What's the context for those variables, though? E.g. `{slug}` in an Section's URL format is really just short for `{entry.slug}`, but unlike URL formats, a generic route doesn't have that kind of context. To give any further advice or suggestions, it'd be helpful to learn more about your actual use case – what kind of routes are your admins going to add, and what kind of variables are you wanting to expose to those routes?  
> **username_0**: Admins will probably use slug, date('y-m-d') & id as most common types of variables.  
> **username_1**: I think you have two options – either write some custom logic in `registerSiteRoutes` to rewrite whatever "bracket variables" the user has added to valid sub patterns, i.e. `{slug}` becomes `(?P<slug>[-\w]+)` (don't see how you can avoid having every sub pattern accept any character, as opposed to having different kinds of sub patterns as per [the official docs](https://craftcms.com/docs/routing#accessing-subpatterns-in-your-templates)). The other option is to have a set of pre-defined variables (i.e. _tokens_), that are replace with different kinds of sub patterns.  
> **username_1**: The latter approach – pre-defined tokens representing various sub patterns – is how the routing UI inside the Craft CP works, so you might check out how P&T built it and try to reuse or recreate some of that functionality.  

---

