3

I'm trying to create a route for an RSS feed which allows for the following routes.

domain/feed
domain/category/feed
domain/category/subcategory/feed

I have the first two routes working and the category fetching logic looks okay to me.

So far in my routes file I have the following

'feed' => 'feed/_index.twig',
'(?P<slug>[^/]+)/feed' => 'feed/_index',

// Additional New route
'(?P<slug>[^/]+)/(?P<slug>[^/]+)/feed' => 'feed/_index',

That last line was added recently as I thought the child category would include the parent category in its route.

I either get a 404 error without that new route, or when I do add that third route I get the following error

preg_match(): Compilation failed: two named subpatterns have the same    
name at offset 27 (craft/app/etc/web/UrlManager.php:387)

Any help would be greatly appreciated.

  • 1
    The error is because you've got two named subpatterns with the same name (<slug>). According to stackoverflow.com/a/7166444/684, you can do that as long as you use the (?J) option in your pattern. Not sure if that's going to solve your overall problem, so just adding this as a comment. – Brad Bell Jul 15 '15 at 2:02
6

Brad nails the problem in his comment:

The error is because you've got two named subpatterns with the same name (<slug>)

And the solution is simple... Change the name(s) of your subpattern(s).

The term slug is just a convention of Craft. It doesn't matter what the names are, they can be anything useful to you:

(?P<domain>[^/]+)/(?P<category>[^/]+)/feed

Whatever values match those named patterns are automatically passed into the Twig template as "magic" variables. So your Twig template can now make use of the domain and category values.

| 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.