2016-03-28 62 views
1

Yii2删除确认对话框不能通过菜单窗口项目工作。Yii2方法不允许。此URL只能处理以下请求方法:POST

[ 
    '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' 
], 

而且我看到这个错误:

Method Not Allowed (#405) Method Not Allowed. This url can only handle the following request methods: 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' 
], 

回答

1

我通过模板选项的固定问题,如下面的代码块:

['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' 
], 
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', 
]; 
0

您发送并GET。但是,在默认情况下删除控制器 - 只发布 发送POST或编辑规则位指示,就像这样:

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

我认为错误的原因是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' 
    ] 
); 
相关问题