nainy
nainy

Reputation: 540

Yii - parsing custom urls

I have a need to parse custom url's into data. The urls might look like this:

http://host.com/categoryXXX/brandYYY/markZZZ/page/1/otherparameters/othervalues

Where the first three parameters are basically prefix+value pairs (category - prefix, XXX - value). They might not all be present except for the first one (category).

All of the other parameters are optional and may or may not be present, and they also come in pairs 'key/value', AKA the usual path format of Yii.

I honestly have no idea how to parse such complex urls. Basically, I need a proper urlManager rule and a working mechanism to parse them.

Will appreciate any suggestions.

Upvotes: 0

Views: 73

Answers (1)

TheNatureBoy
TheNatureBoy

Reputation: 188

/\/category(?P<category_id>[^\/]*)(?:\/brand(?P<brand_id>[^\/]*))?(?:\/mark(?P<mark_id>[^\/]*))?/i

This will create named captures for all of the fields you are looking for. brand_id and mark_id are optional

Working version: http://regex101.com/r/fQ7mU0/3

Upvotes: 1

Related Questions