5

I want to move some permanent routes from the admin to my routes config file so that they can be version controlled.

in the admin...
news-sort/month/<year>/<month> ....
news-sort/tag/<tag> ....
news-sort/author/<*>

Do these look ok, not 100% sure on the variable and regex etc.

'news-sort/month/(?P<year>\d{4})/(?P<month>\d{2})' => 'news-sort/index',
'news-sort/tag/(?P<tag>[a-zA-Z0-9._%-]+)' => 'news-sort/index',
'news-sort/author/(?P<author>[a-zA-Z0-9._%-]+)' => 'news-sort/index'
  • Do they work, mjr? – carlcs Jul 29 '14 at 16:07
  • I tested one and it seems to work ok – mjr Jul 29 '14 at 17:42
9

The simplest way to transform dynamic routes into advanced routes, is to go to your craft_routes table and copy it out of urlPattern:

Example:

If you have set this in your backend:
news-sort/month/<year>/<month>

You'll find this in urlPattern:
news\-sort\/month\/(?P<year>\d{4})\/(?P<month>(?:0?[1-9]|1[012]))

Just remove the escaping \, and you've got your regex:
news-sort/month/(?P<year>\d{4})/(?P<month>(?:0?[1-9]|1[012]))

| improve this answer | |
  • great idea, I never thought of that! – mjr Jul 29 '14 at 17:41
  • 1
    awesome answer! – Sean O'Grady Jul 29 '14 at 19:48
3

You forgot to escape the dot, but otherwise it looks good to me.

You could replace [a-zA-Z0-9._%-] with . to allow all characters in the slug. Maybe you want to use german umlauts or other special characters in your slugs at some point (you probably shouldn't, but who knows). I'd do that (the ., not the umlauts), as I think it's not the task of a route to restrict you in doing such things.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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