Problem with UrlManager rules

Hello! I am trying to define rules for REST API. I have three rules for actions like CREATE, VIEW and DELETE, so I decided to use HTTP verb to identify them. Here is urlManager array:




'urlManager' => array(

  'showScriptName' => true,

  'urlFormat' => 'path',


  'rules' => array(

    array(

      'default/index',

      'pattern' => ''

    ),

    /* Create */

    array(

      '<controller>/create',

      'pattern' => '<controller:\w+>',

      'verb' => 'POST'

    ),

    /* Delete */

    array(

      '<controller>/delete',

      'pattern' => '<controller:\w+>/<id:\d+>',

      'verb' => 'DELETE'

    ),

    /* View */

    array(

      '<controller>/view',

      'pattern' => '<controller:\w+>/<id:\d+>',

      'verb' => 'GET'

    ),

  ),

)



But if I make DELETE request, Controller::actionView() executes, same with GET request (that should do it).

I am new to Yii.

Thanks for help!

I don’t see an error at first sight (despite the “patern” typo in your first rule) but maybe you still have a default route in your config some were? I am asking because the rules Yii creates by default would route “controller/id” to “controller/view”.

I believe the view action applies for both your intended delete and your actual view actions because their patterns are the same. Fortunately you didn’t invert their entries: the delete action would then apply in both cases.

Simple solution: change the delete entry’s pattern.

Yes, I have that default rule:


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

Don’t know if I can remove it, because web app has non-REST part, written by other people.

The problem is that I can not call Controller::actionDelete() with DELETE request (it calls Controller::actionView(), that should be called with GET verb).

Thanks for the typo, I fixed it :)

Then try to move this rule at the end of your rules. The urlManager goes down the list and as soon as it finds a matching rule it is applied. So if the mentioned one comes first and you didn’t define a verb (which you didn’t in this case) it would also match DELETE requests.

By the way. I don’t see any reason for this additional rule. It is actually doing the same as your one with the GET rule doesn’t it? Meaning it leads to the same controller/action

Or not… I’m not sure anymore :\

I thought about that one too but if you rethink it it would be a horrible bug. Because most people would never think about the possibility of routing a GET to a DELETE action just because the order of the entries is wrong. If that would be the case it would be really bad, hope it is not the case :)

Yes I was wrong sorry (emphasis mine):

http://www.yiiframework.com/doc/guide/1.1/en/topics.url

Thank you, I think I got it after your explanation. I did something like "override" in config (used main app config, merged with my specific one). So I think, I should define my config without it, because REST and non-REST parts of app use different entries.

I see, I would recommend creating a separate module for your REST api. This way would have distinct routes like "api/controller/action"