Search code examples
phppostyii2confirm

yii2 delete record error


i getting error while deleting record in all table:

An Error occurred while handling another error:
exception 'yii\web\ForbiddenHttpException' with message 'You are not allowed to perform this action.' in D:\FORBIDDEN\projects\UniServerZ\www\project\vendor\yiisoft\yii2\filters\AccessControl.php:151
Stack trace:

Previous exception:
exception 'yii\web\MethodNotAllowedHttpException' with message 'Method Not Allowed. This url can only handle the following request methods: POST.' in D:\FORBIDDEN\projects\UniServerZ\www\project\vendor\yiisoft\yii2\filters\VerbFilter.php:105
Stack trace:

_

here my controller:

public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }


public function actionDelete($id)
{
   $this->findModel($id)->delete();
   return $this->redirect(['index']);
}

and my delete button in view.php

<?= Html::a('Delete', ['delete', 'id' => $model->id_transaksi], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) 
?>


its working when i replaced POST with GET, but alert confirm not working.. do you know what's wrong? help, please


Solution

  • It might be that you get the error message

    Method Not Allowed. This url can only handle the following request methods: POST.
    

    because somehow the browser reloads the page with requested url. When you click on the link to delete that model, the request method is POST, as it is specified. At that point some exception occurs (seems to be related to access rules) that makes you browser forgive about the request method.