将令牌添加到Update操作Yii2的url

时间:2018-03-16 14:55:31

标签: yii2-advanced-app

我想知道在哪里可以将token参数添加到后端的url中?我的网址现在看起来是www.domain.com/admin/brand/update?id=26,我只是想在最后添加一个令牌以防止像www.domain.com/admin/brand/update?id=26&token=23782173812这样的缓存。但是现在在这一刻我意识到我不知道那个网址是在哪里构建的。这是我的控制器动作:

public function actionUpdate($id)
    {
        $model = $this->findModel($id, false);
        $settings = new Settings;
        if ($model->load(Yii::$app->request->post())) {
            $languages = Lang::find()->all();
            foreach ($languages as $language) {
                if ($language->default != 1) {

                    $title = 'title_' . $language->url;
                    $varTitle = Yii::$app->OutData->sanitize($model->$title);
                    $model->$title = $varTitle;

                    $description = 'description_' . $language->url;
                    $varDescription = Yii::$app->OutData->sanitize($model->$description);
                    $model->$description = $varDescription;
                } else {
                    $model->title = Yii::$app->OutData->sanitize($model->title);
                    $model->description = Yii::$app->OutData->sanitize($model->description);
                }
            }
            $model->link = Yii::$app->OutData->sanitize($model->link);

            if ($model->save()) {
                $model = $this->findModel($model->id, true);

                foreach ($languages as $language) {
                    if ($language->default != 1) {
                        $urlName = 'url_' . $language->url;
                        $model->$urlName = MakeURL::parseUrlGoogle($model->$urlName, $model->id);
                    } else {
                        $model->url = MakeURL::parseUrlGoogle($model->url, $model->id);
                    }
                }
                $model->update();

                $model->setImageFolder();
                $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
                if (isset($model->imageFiles) and count($model->imageFiles) > 0) {
                    foreach ($model->imageFiles as $key => $file) {
                        $file->saveAs(Yii::getAlias('@frontend/web') . '/brands_images/'.$model->getImageFolderName().'/brand_' . $model->id . '_' . ($key + 1) . '.' . $file->extension);

                        $pic = Yii::getAlias('@frontend/web') . '/brands_images/'.$model->getImageFolderName().'/brand_' . $model->id . '_' . ($key + 1) . '.' . $file->extension;
                       /* $settings->compress($pic, $pic, 90);*/

                    }
                }
                return $this->redirect(['index']);
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

如果需要我的表格:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use frontend\modules\storeadmin\models\Lang;
use frontend\components\OutData;
use kartik\file\FileInput;

/* @var $this yii\web\View */
/* @var $model backend\models\News */
/* @var $form yii\widgets\ActiveForm */

$_SESSION['KCFINDER'] = array(
    'disabled' => false,
    'uploadURL' => "/media",
    'uploadDir' => Yii::getAlias('@frontend/web') . "/media/",
);
?>

<div class="news-form">

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
    <?php $languages = Lang::find()->all(); ?>

    <ul class="nav nav-tabs">
        <?php
        foreach ($languages as $key => $language) {
            if ($language->default == 1) {
                echo '<li class="active"><a data-toggle="tab" href="#' . $key . '">' . $language->name . '</a></li>';
            } else {
                echo '<li><a data-toggle="tab" href="#' . $key . '">' . $language->name . '</a></li>';
            }
        }
        ?>
    </ul>

    <div class="tab-content">
        <?php
        foreach ($languages as $key => $language) {
            if ($language->default != 1) {
                echo '<div id="' . $key . '" class="tab-pane fade">';
                echo $form->field($model, 'title_' . $language->url)->textInput(['maxlength' => true]);
                $description = 'description_' . $language->url;
                $model->$description = OutData::showTXT($model->$description);
                echo $form->field($model, 'description_' . $language->url)->textarea(['rows' => 6]);
                echo $form->field($model, 'url_' . $language->url)->textInput(['maxlength' => true]);
                echo "<script>
                        CKEDITOR.replace( 'News[description_$language->url]' );
                    </script>";
            } else {
                echo '<div id="' . $key . '" class="tab-pane fade in active">';
                echo $form->field($model, 'title')->textInput(['maxlength' => true]);
                $model->description = OutData::showTXT($model->description);
                echo $form->field($model, 'description')->textarea(['rows' => 6]);
                echo $form->field($model, 'url')->textInput(['maxlength' => true]);
                echo "<script>
                        CKEDITOR.replace( 'News[description]' );
                    </script>";
            }
            echo '</div>';
        }
        ?>
        <div class="row">
            <div class="col-sm-12">
                <?= $form->field($model, 'link')->textInput() ?>
            </div>
        </div>
    </div>
    <hr>
        <h3><strong>Размер: 220w : 100h</strong></h3>
    <hr>
    <?php 
        $newsImages = array();
        $count = 0;
        for ($i = 1; $i < Yii::$app->params['numberNewsPhotos'] + 1; $i++) {
            if ($model->hasImg($i)) {
                $count++;
                $newsImages[] = Html::img($model->getImg($i), ['class'=>'file-preview-image', 'alt'=>$model->title, 'title'=>$model->title]).
                       '<a href="javascript://" onclick="deleteBrandsImage(' . $model->id . ',' . $i . ')"><span class="glyphicons glyphicons-bin"></span></a>';
            }
        }
    ?>
    <?=
    $form->field($model, 'imageFiles[]')->widget(FileInput::classname(), [
        'options' => [
            'multiple' => true,
            'disabled' => ((Yii::$app->params['numberBrandsPhotos'] - $count) == 0) ? true : false,
        ],
        'pluginOptions' => [
            'initialPreview' => $newsImages,
            'allowedFileExtensions'=> Yii::$app->params['allowedNewsFileExtensions'],
            'maxFileCount' => Yii::$app->params['numberBrandsPhotos'] - $count,
        ],
    ]);
    ?>
    <div class="row">
        <div class="col-sm-6">
            <?= $form->field($model, 'sort')->textInput() ?>
        </div>
        <div class="col-sm-6">
        <?= $form->field($model, 'active')->dropDownList(['0' => Yii::t('app', 'Не'), '1' => Yii::t('app', 'Да')]) ?>
        </div>
    </div>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'app.Create') : Yii::t('app', 'app.Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<script>
    $('#w0').on('beforeValidate', function (event, messages, deferreds) {
        for (var instanceName in CKEDITOR.instances) {
            CKEDITOR.instances[instanceName].updateElement();
        }
        return true;
    });

    function deleteBrandsImage(brands_id, key) {
        var r = confirm("Сигурни ли сте че искате да изтриете изображението");
        if (r == true) {
            $.ajax({
                url: '/storeadmin/brands/deleteimage',
                method: "POST",
                data: {
                    brands_id: brands_id,
                    key: key,
                },
                success: function (data) {
                    location.reload();
                }
            });
        }
    }

</script> 

0 个答案:

没有答案