Yii Not able to route requests to different paths

89 views Asked by At

I have a problem while routing my requests in Yii. Following is the urlManager rules:

'rules'=>array(

                array('api/index', 'pattern'=>'api/<model:\w+>/<id:\d+>/*', 'verb'=>'GET'),
                array('api/shortlist', 'pattern'=>'api/<action>/<model:\w+>/*', 'verb'=>'GET'),
                array('api/compare', 'pattern'=>'api/<action>/<model:\w+>/*', 'verb'=>'GET'),

                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),

My requests get routed to api/shortlist even if I hit the api/compare link.

What am I doind wrong here?

1

There are 1 answers

0
Samuel Liew On BEST ANSWER

As you can see, the patterns for your shortlist and compare rules are the same.

Therefore, the rule that is placed first will match for both, and the second one will never run.

Your api/compare rule will never match/hit because the rule above it is the same and will match first.

You should have done something like this to prevent conflict:

'api/shortlist' => 'api/shortlist/<action>/<model:\w+>/*',
'api/compare' => 'api/compare/<action>/<model:\w+>/*',