1 | <?php |
||
2 | /** |
||
3 | * @author Anton Tuyakhov <[email protected]> |
||
4 | */ |
||
5 | |||
6 | namespace tuyakhov\jsonapi\actions; |
||
7 | |||
8 | use Yii; |
||
9 | use yii\db\BaseActiveRecord; |
||
10 | use yii\web\NotFoundHttpException; |
||
11 | use yii\web\ServerErrorHttpException; |
||
12 | |||
13 | /** |
||
14 | * Implements the API endpoint for deleting resources. |
||
15 | * @link http://jsonapi.org/format/#crud-deleting |
||
16 | */ |
||
17 | class DeleteAction extends Action |
||
18 | { |
||
19 | /** |
||
20 | * Deletes a resource. |
||
21 | * @param mixed $id id of the resource to be deleted. |
||
22 | * @throws NotFoundHttpException |
||
23 | * @throws ServerErrorHttpException on failure. |
||
24 | */ |
||
25 | public function run($id) |
||
26 | { |
||
27 | /** @var BaseActiveRecord $model */ |
||
28 | $model = $this->findModel($id); |
||
29 | |||
30 | if ($this->checkAccess) { |
||
31 | call_user_func($this->checkAccess, $this->id, $model); |
||
32 | } |
||
33 | |||
34 | if ($model->delete() === false) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
35 | throw new ServerErrorHttpException('Failed to delete the resource for unknown reason.'); |
||
36 | } |
||
37 | |||
38 | Yii::$app->getResponse()->setStatusCode(204); |
||
39 | } |
||
40 | } |