Yii2通过后端模型从前端通过gridview中的超链接删除会给出错误的请求(#400)
我有一个名为features的表的更新表单,并在其上放置了一个gridview::小部件来处理相关的图像。我可以通过更新按钮更新图像,但当我尝试删除时,会收到错误的请求消息 我的代码为gridview删除图标生成以下代码:Yii2通过后端模型从前端通过gridview中的超链接删除会给出错误的请求(#400),gridview,yii2,frontend,backend,bad-request,Gridview,Yii2,Frontend,Backend,Bad Request,我有一个名为features的表的更新表单,并在其上放置了一个gridview::小部件来处理相关的图像。我可以通过更新按钮更新图像,但当我尝试删除时,会收到错误的请求消息 我的代码为gridview删除图标生成以下代码: <a href="http://gb.n/image/delete/16" title="Delete" aria-label="Delete" data-confirm="Are you sure that you want to delete
<a href="http://gb.n/image/delete/16"
title="Delete"
aria-label="Delete"
data-confirm="Are you sure that you want to delete this image??"
data-method="post" data-pjax="0">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</a>
我已将此添加到ImageController。我在堆栈溢出的其他地方找到了它:
public function beforeAction($action) {
if ($action->id == 'delete') {
Yii::$app->controller->enableCsrfValidation = false;
}
return parent::beforeAction($action);
}
你(你的链接)如何引用后端??我无法理解你的链接…你好,斯佳芝,谢谢你的提问。你好,斯佳芝。该链接位于前端页面上,我使用Html::a创建url,如下所示:
Yii::$app->urlManagerBackend->baseUrl/image/delete/”$型号->id
谢谢。请查看CSRF设置!这可能会解决您的问题。请尝试在控制器操作中禁用csrf,然后查看。
public function actionDelete($id) {
$model = $this->findModel($id);
try {
unlink($model->image_path);
unlink($model->thumb_path);
unlink($model->mobile_path);
$model->delete();
return $this->redirect(['index']);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage());
}
}
public function beforeAction($action) {
if ($action->id == 'delete') {
Yii::$app->controller->enableCsrfValidation = false;
}
return parent::beforeAction($action);
}