Yii2方法不允许。此URL只能处理以下请求方法:POST

时间:2016-03-28 22:34:43

标签: yii2

Yii2删除确认对话框无法通过菜单小部件项目工作。

[
    'label' => '<i class="fa fa-trash-o alis"></i> Sil',
    'url' => ['site/delete', 'id' => $model->id],
    'linkOptions' => [
        'data-confirm' => 'Are you sure you want to delete this item?',
        'data-method' => 'post',
    ],
    'visible' => 'visible'
],

我看到了这个错误:

  

不允许的方法(#405)   方法不允许。此URL只能处理以下请求方法:POST。

如何使用删除确认对话框。然后我尝试这个但不工作......

[
    'label' => '<i class="fa fa-trash-o alis"></i> delete',
    'url' => ['site/delete','id' => $model->id],
    [
        'data' =>[
            'data-confirm' => 'Are you sure you want to delete this item?',
            'data-method' => 'post',
        ],
    ],
    'visible' => 'visible'
],

4 个答案:

答案 0 :(得分:2)

我通过模板选项修复了问题,例如下面的代码块:

['label' => '<i class="fa fa-trash-o alis"></i> delete',
    'url' => ['site/delete','id' => $model->id],
    'template' => '<a href="{url}" data-confirm = "Are you sure you want to delete this item?", data-method="post">{label}</a>',
    'visible' => 'visible'
],

答案 1 :(得分:0)

视图文件中的

['label' => '<i class="fa fa-trash-o alis"></i> delete',
   'url' => ['site/delete','id' => $model->id],
    'template' => '<a href="{url}" data-confirm = "Are you sure you want to delete this item?", data-method="post">{label}</a>',
   'visible' => 'visible'

],

并且在AppAsset文件中我们必须激活依赖于这样的数组:

public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
];

答案 2 :(得分:0)

你发送并获取。但是在控制器中默认为删除 - 只有POST 在控制器中发送POST或编辑规则,如下所示:

public function behaviors()
{
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'], //delete this string to may GET
            ],
        ],
    ];
}

答案 3 :(得分:0)

我认为错误的原因是当您使用为控制器的函数behavior()中的操作配置的方法单击链接差异时的HTTP方法。所以你需要为链接定义方法(对不起,我的英语不好)。我尝试过它有效:

Html::a('', $url,
  [
     'data' => [
         'method' => 'post',
          // use it if you want to confirm the action
          'confirm' => 'Are you sure?',
      ],
      'class' => 'glyphicon glyphicon-trash btn btn-default btn-xs custom_button'
   ]
 );