【问题标题】:yii2 delete record erroryii2删除记录错误
【发布时间】:2016-06-29 04:28:11
【问题描述】:

删除所有表中的记录时出错:

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:

_

这是我的控制器:

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


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

和 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',
            ],
        ]) 
?>


当我用 GET 替换 POST 时它工作,但警告确认不工作.. 你知道怎么了?请帮忙

【问题讨论】:

  • GET的情况下删除method,因为url已经发出了GET请求
  • 你能验证你的 Yii 资产是否被正确地包含进来了吗?我的意思是请确认它是否真的触发了 POST 请求

标签: php post yii2 confirm


【解决方案1】:

可能是您收到错误消息

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

因为浏览器会以某种方式重新加载带有请求 url 的页面。当您单击链接以删除该模型时,请求方法是 POST,因为它是指定的。此时会发生一些异常(似乎与访问规则有关),使您的浏览器原谅请求方法。

【讨论】:

    【解决方案2】:
    public function behaviors()
        {
            return [
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        'delete' => ['post'],//not POST
                    ],
                ],
            ];
        }
    

    笔误,希望对你有帮助 参考链接 http://www.yiiframework.com/doc-2.0/yii-filters-verbfilter.html

    【讨论】:

      猜你喜欢
      • 2017-12-22
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多