yii2 删除 GridView 时:无法验证您的数据提交

yii2 When delete GridView : Unable to verify your data submission

当我想删除 GridView 上的项目时出现此错误:

exception 'yii\web\BadRequestHttpException' with message 'Unable to verify your data submission.

我这是我的控制器代码:

class DevisController extends Controller
{
 public $layout = 'lay-admin';

 public function behaviors()
 {
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
    ];
}
/* ..... */
public function actionDelete($id)
{
    $this->findModel($id)->delete();

    return $this->redirect(['index']);
}

当我将行为函数中的 post 方法更改为 get 方法时,出现此错误

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

网格视图代码:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],

                    //'idDevis',
                    'reference',
                    'client',
                    'dateCreation',
                    'contact',
                    'valableJusqua',
                    'dateRelance',
                    [
                     'attribute'=>'etat',
                     'filter'=>ArrayHelper::map(Devis::find()->asArray()->all(), 'etat', 'etat'),
                    ],
                    'commercial',
                    'modePaiement',
                    'delaiPaiement',

                    ['class' => 'yii\grid\ActionColumn'],
                ],
]); ?>

有什么想法吗!!

在您的自定义布局文件中添加 CSRF 元标记。

示例:

<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?= Html::csrfMetaTags() ?>

    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

Here you can read more about CSRF.