问题
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