Yii2从前端通过后端模型通过gridview中的超链接删除给出错误请求(#400)

时间:2016-01-20 14:25:53

标签: gridview yii2 frontend backend bad-request

我有一个名为features的表的更新表单,并在其上放置了gridview :: widget来处理相关的图像。我可以通过更新按钮更新图像,但是当我尝试删除时,我收到错误的请求消息。

我的代码为gridview删除图标生成以下代码:

<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>

“功能”表单位于前端,图像mvc位于我的“高级”模板实现的后端。

在寻找解决方案后,我怀疑它与cookie有关,但由于我是Yii和MVC框架的新手,它可能是任何东西。

我的环境是Win 7,Apache 2.4.4。

根据要求提供更多信息:

<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'defaultRoute' => 'pages/index',
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'urlManager' => ['class' => 'yii\web\UrlManager',
//            // Disable index.php 
            'showScriptName' => FALSE,
//            // Disable r= routes 
            'enablePrettyUrl' => true,
            'rules' => [
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                '<controller:\w+\-\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<id:\d+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/view',
            ],
        ],
        //can name it whatever you want as it is custom
        'urlManagerBackend' => [
            'class' => 'yii\web\urlManager',
            'baseUrl' => 'http://gb.n',
            'enablePrettyUrl' => true,  
            'showScriptName' => false,
        ],
    ],
    'params' => $params,
];

ImageController的actionDelete方法:

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());
        }
    }

我已将此添加到ImageController中。我在堆栈溢出的其他地方找到了它:

public function beforeAction($action) {
    if ($action->id == 'delete') {
        Yii::$app->controller->enableCsrfValidation = false;
    }

    return parent::beforeAction($action);
}

0 个答案:

没有答案