DeleteAction.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\rest;
  8. use Yii;
  9. use yii\web\ServerErrorHttpException;
  10. /**
  11. * DeleteAction implements the API endpoint for deleting a model.
  12. *
  13. * For more details and usage information on DeleteAction, see the [guide article on rest controllers](guide:rest-controllers).
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. class DeleteAction extends Action
  19. {
  20. /**
  21. * Deletes a model.
  22. * @param mixed $id id of the model to be deleted.
  23. * @throws ServerErrorHttpException on failure.
  24. */
  25. public function run($id)
  26. {
  27. $model = $this->findModel($id);
  28. if ($this->checkAccess) {
  29. call_user_func($this->checkAccess, $this->id, $model);
  30. }
  31. if ($model->delete() === false) {
  32. throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
  33. }
  34. Yii::$app->getResponse()->setStatusCode(204);
  35. }
  36. }