DELETE verb is not working in Yii Rest Api?

丶灬走出姿态 提交于 2020-01-05 04:40:39

问题


When try to delete a record using Yii Rest api

HTTP DELETE mothod is not working

my url routing is

array('users/list', 'pattern'=>'users/<model:\w+>/<id:\d+>', 'verb'=>'DELETE'), 

when i request like

http://localhost/api/users/delete?id=1

in method DELETE in rest client

it says there is no method like delete

so i created an action like the following

public function actionDelete()
    {


        switch($_GET['action'])
        {
        case 'delete': // {{{
                $id = $_GET['id'];
                $this->DeleteUser($id);
                break; // }}}
            default:
                    break;
        }
}

Now it says undefined index : model

My understanding is if we use HTTP DELETE method we should have an action called actionDelete right?

How to fix this ?


回答1:


verb => 'DELETE' means that the request method is delete.

You should be able to call your api with the following javascript:

$.ajax({
    url: 'index.php/api/users/1',
    type: 'DELETE',
    async: false,
    success: function(result) {
      alert('Deleted');
    },
    error: function(result) {
      alert('Could not delete!');
    }
});

Do you have the standard filters active? If yes, you should remove "postOnly + delete".

/**
 * @return array action filters
 */
public function filters()
{
    return array(
        'accessControl', // perform access control for CRUD operations
        'postOnly + delete', // we only allow deletion via POST request
    );
}


来源:https://stackoverflow.com/questions/20918336/delete-verb-is-not-working-in-yii-rest-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!