Dynamic Url Patterns

Hello,

I’m trying to achieve a dynamic URL management system for an API.

Right now I have something like this:


		array('api/users/list', 'pattern'=>'api/users/list<format:(.xml|.json)>?', 'verb'=>'GET'),

                array('api/users/view', 'pattern'=>'api/users/<id:\d+><format:(.xml|.json)>?', 'verb'=>'GET'),

                array('api/users/count', 'pattern'=>'api/users/count<format:(.xml|.json)>?', 'verb'=>'GET'),

And I want to have that for users, companies, etc., but I don’t want to do URL filters to every type of data I need.

I think it could be done by doing something like this:


		array('api/<controller:\w+>/list', 'pattern'=>'api/<controller:\w+>/list<format:(.xml|.json)>?', 'verb'=>'GET'),

                array('api/<controller:\w+>/view', 'pattern'=>'api/entities/<id:\d+><format:(.xml|.json)>?', 'verb'=>'GET'),

                array('api/<controller:\w+>/count', 'pattern'=>'api/entities/count<format:(.xml|.json)>?', 'verb'=>'GET'),

But it doesn’t work as this way I’ve wrote.

Any suggestions?

"route" part should contain only variable names without regexp, like:




array('api/<controller>/list', 'pattern'=>'api/<controller:\w+>/list<format:(.xml|.json)>?', 'verb'=>'GET'),

array('api/<controller>/view', 'pattern'=>'api/<controller:\w+>/<id:\d+><format:(.xml|.json)>?', 'verb'=>'GET'),

array('api/<controller>/count', 'pattern'=>'api/<controller:\w+>/count<format:(.xml|.json)>?', 'verb'=>'GET'),



also you should put "<controller:\w+>" in every "pattern" (like above).

Exactly!

Thanks a lot @redguy!

Btw, what is the explanation of “w+” in front of controller in the pattern? For being a regex, shouldn’t it be something like [a-Z] p.ex.?

it is alias to [:word:] character class (http://php.net/manual/en/regexp.reference.character-classes.php). the "pattern" parameter must be valid regexp which can contain named parameters - there is no Yii magic in it.