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?

1 Answer 1


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:

    'some-custom-route' => '_some-custom-template',
    'another-custom-route' => '_another-custom-template',
  • If the route has brackets for tokens will Craft parse that or does it need to be converted somehow?
    – a-am
    Commented Aug 18, 2016 at 15:10
  • Unlike URL formats for entries and categories, routes declared with registerSiteRoutes() use sub patterns for parameters and won't parse Twig or URL format tokens (i.e. {slug}). What kind of variables do you need in your routes? Commented Aug 18, 2016 at 15:28
  • 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
    Commented Aug 18, 2016 at 15:35
  • 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? Commented Aug 18, 2016 at 19:05
  • Admins will probably use slug, date('y-m-d') & id as most common types of variables.
    – a-am
    Commented Aug 18, 2016 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.