Settings page edit

Hello!

I need edit page for change some settings (no option for delete/create/view, only update existing fields and save - that’s all)

Example of edit page (route will be for example /settings/, default and only one view will be index):

http://s23.postimg.org/4q5zpic8r/setting_view.png

Table structure (key x value):

http://s22.postimg.org/o7okix081/setting_db.png

In the view I want to be able to change fields settings (name, type, etc.) for each entry.

Can anyone help me with this?

I found some plugins, but they have little different usage.

I got it work but with table structure: title, description, keyword :(

Its possible to get same working with table structure key x value?

Page preview is in first post.

model:


<?php


namespace backend\modules\setting\modules\seo\models;


use Yii;

use yii\db\ActiveRecord;


class Seo extends ActiveRecord

{


	public $controllerNamespace = 'backend\modules\setting\modules\seo\controllers';


	public static function tableName()

	{

		return '{{%seo}}';

	}


	public function rules()

	{

		return [

			[['title', 'description', 'keyword'], 'trim'],

			[['title'], 'required', 'message' => Yii::t('backend', 'The title field is required.')],

			[['title'], 'string', 'max' => 64, 'message' => Yii::t('backend', 'The title field can not be longer than 64 characters.')],

			[['description'], 'string', 'max' => 256, 'message' => Yii::t('backend', 'The description field can not be longer than 256 characters.')],

			[['description'], 'string', 'max' => 256, 'message' => Yii::t('backend', 'The keywords field can not be longer than 64 characters.')],

        ];

	}


	public function attributeLabels()

	{

		return [

			'title' => Yii::t('backend', 'Title'),

			'description' => Yii::t('backend', 'Description'),

			'keyword' => Yii::t('backend', 'Keywords'),

		];

	}


}

controller:


<?php


namespace backend\modules\setting\modules\seo\controllers;


use Yii;

use yii\web\Controller;

use yii\data\ActiveDataProvider;

use yii\filters\VerbFilter;

use yii\filters\AccessControl;

use backend\modules\setting\modules\seo\models\Seo;


class SeoController extends Controller

{


	public function behaviors()

	{

		return [

			'access' => [

				'class' => AccessControl::className(),

				'rules' => [

					[

						'allow' => true,

						'roles' => ['@'],

					],

				],

			],

		];

	}


	public function actionIndex()

	{

        $model = Seo::findOne(0);


		if ($model->load(Yii::$app->request->post()))

		{

			if ($model->validate())

			{

				if ($model->save(false))

				{

					Yii::$app->getSession()->setFlash('success', [

						'type' => 'success',

						'icon' => 'glyphicon glyphicon-ok-sign',

						'message' => Yii::t('backend', 'SEO settings have been saved.'),

						'title' => Yii::t('common', 'Success!'),

					]);

					return $this->redirect(['/settings/seo']);

				} else

				{

					Yii::$app->getSession()->setFlash('error', [

						'type' => 'danger',

						'icon' => 'glyphicon glyphicon-remove-sign',

						'message' => Yii::t('backend', 'SEO settings could not be saved.'),

						'title' => Yii::t('common', 'Error!'),

					]);

					return $this->refresh();

				}

			}

		}


        return $this->render('index', [

            'model' => $model,

        ]);

	}


}

view:


<?php

use yii\helpers\Html;

use yii\widgets\ActiveForm;

?>


<div class="default-index seo">

    <?php $form = ActiveForm::begin(['id' => 'form-seo']); ?>

        <?= $form->field($model, 'title')->textInput() ?>

        <?= $form->field($model, 'description')->textInput() ?>

        <?= $form->field($model, 'keyword')->textInput() ?>


        <div class="form-group">

    		<?= Html::submitButton(Yii::t('common', 'Update'), ['class' => 'btn btn-success']) ?>

        </div>

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

</div>

Bump

If you want only "key" and "value" pairs, then you can create a table that only has "key" and "value" columns. Then

  1. Run Gii’s Model Generator for that table.

  2. Run Gii’s CRUD Generator for the model.

  3. Use “update” page of the created CRUD … if you don’t want other actions like “index”, “view”, “create”, you can simply delete them.