📜 ⬆️ ⬇️

Simple settings page for Yii application

Hey.

I want to share a small module designed to create a settings page in my application. But at the same time learn from the community that it could be improved.

The task in its idea is simple - to make a table in the database that stores parameters of the type “key -> value”, and to it make an editing page that allows changing these values. and, of course, implement a software interface for accessing parameters stored in the database.
')
The implementation period is about half an hour. For some reason, there was no ready-made solution on the Internet. We will fix the situation, maybe someone will come in handy.

We will do on a specific example. Suppose we need to store such settings: Application Name, Application Description, Password for Gii, Email Administrator.

We write


So, first create a directory structure in which we add our module:
configpage components Econfig.php conrollers WtsController.php models WtsConfigForm.php views wts config.php configForm.php ConfigpageModule.php 

We will not invent anything and use ready-made extensions for storing settings in the database - EConfig . His and put in the catalog components. This extension has two methods “get” and “set”, and also is able to create a table in the database itself, that is, it will not be necessary to bathe in its setting.

The template for our module is generated via Gii and the main file of the ConfigpageModule.php module remains unchanged:
 class ConfigpageModule extends CWebModule { public function init() { $this->setImport(array( 'configpage.models.*', 'configpage.components.*', )); } public function beforeControllerAction($controller, $action) { if(parent::beforeControllerAction($controller, $action)) { return true; } else return false; } } 

Much more interesting things with the form itself. To create it, we can use the form designer included in the Yii delivery. Read more about him here .

In short, we need to create a form model, a controller, a form description, and a view. Let's start with the model.
WtsConfigForm.php:
 class WtsConfigForm extends CFormModel { public $applicationName; public $applicationShortDesc; public $giiPassword; public $adminEmail; public function rules() { return array( array('applicationName, giiPassword, adminEmail, applicationShortDesc', 'required'), ); } public function attributeLabels() { return array( 'applicationName' => Yii::t('app', ' '), 'applicationShortDesc' => Yii::t('app', '  '), 'giiPassword' => Yii::t('app', '    Gii'), 'adminEmail' => Yii::t('app', 'Email '), ); } } 

Everything is simple - they described 4 fields, described their labels and indicated a rule stating that the field is mandatory. Hint: if you do not specify a field as required, it will not appear on the form.

Okayno. Leave the controller for a sweet, but for now let's describe the shape and view. The form description is a simple array that stores the description of fields and form controls.
configForm.php:
 return array( 'elements'=>array( 'mainSettings'=>array( 'type'=>'form', 'title'=>' ', 'elements'=>array( 'applicationName'=>array('type'=>'text',), 'applicationShortDesc'=>array('type'=>'text',), ), ), 'systemSettings'=>array( 'type'=>'form', 'title'=>' ', 'elements'=>array( 'adminEmail'=>array('type'=>'text',), 'giiPassword'=>array('type'=>'text', ), ), ), ), 'buttons'=>array( 'configPage'=>array( 'type'=>'submit', 'label'=>'', ), ), ); 

Simply? So I think so. We have identified two groups of settings - the main and system, in each group described some fields that are present in the model and located the button “Save” from below.

Output all this we will be the simplest view, whose name is config.php :
 <?php $this->breadcrumbs = array( '', ' ', ); ?> <div class="form"> <?php echo $form; ?> </div> 

Well, everything is completely trivial. Is that for the beauty leave breadcrumbs.

But now will be the most interesting. In the controller, we associate our model with an arbitrary number of fields and specials. DB signboard that stores key-> value pairs. Communication as I said will be folded EConfig module. File WtsController.php :
 class WtsController extends Controller { public function actionIndex() { $model = new WtsConfigForm; foreach ($model->attributes as $attr => $val) { $model->$attr = Yii::app()->getModule('configpage')->config->get($attr); } $form = new CForm('configpage.views.wts.configForm', $model); if ($form->submitted('configPage') && $form->validate()) { foreach ($model->attributes as $attr => $val) { Yii::app()->getModule('configpage')->config->set($attr, $val); } $this->render('config', array('form' => $form)); } else { $this->render('config', array('form' => $form)); } } } 

What's going on here. In actionIndex (so as not to produce too much) we create an instance of the model, fill it with values ​​from the database, and if the required value is not in the database, then null is entered in the field, then we create the form using our description and model instance.

And then we check - if there is data from the browser - if there is any, then the model will automatically load it, after which this data will be recorded in the database and the settings page will be shown to the user again.

By the way, yes, note that I did not describe the filtering rules for users as superfluous. You do not forget to do this, otherwise everyone will be able to write settings to the database.

There are trifles. Connect and start using the module.

We connect


Connecting the module is easy. Just enter the following code in the modules section of the main application configuration file ( main.php ).
 ... 'modules' => array( 'configpage' => array( 'components' => array( 'config' => array( 'class' => 'EConfig', 'strictMode' => false, ), ), ), ), ... 

That's it, the module is connected.

We use


Naturally, the page itself has not fallen off to us, we need some way to access the information stored in the database. For this particular example, we can implement the data loading in the index.php file of our application:
 $app = Yii::createWebApplication($config); function loadSettings() { Yii::app()->getModule('gii')->password = Yii::app()->getModule('configpage')->config->get('giiPassword'); Yii::app()->name = Yii::app()->getModule('configpage')->config->get('applicationName'); Yii::app()->params['adminEmail'] = Yii::app()->getModule('configpage')->config->get('adminEmail'); } loadSettings(); $app->run(); 

That is, we create an instance of the application, it loads the modules and settings, then we overwrite some of the settings and only after that we execute the application.
Something like this. The only thing I don’t like is the get method call string:
 Yii::app()->getModule('configpage')->config->get('applicationName'); 

It is too long. Unfortunately, I cannot figure out how to shorten it. Can someone tell me?

Well, where do without pictures. This is what we should get in the end:
image

By the way, do not scold the picture if you find similarities with this project. OpenSource, all things, you can use other people's work. In this case, I liked the location of the panel on the left. Otherwise no resemblance =). But in general, Twitter Bootstrap is the same everywhere.

From positive


I am a specialist in specialty nifiga not a web developer, but for some reason lately I have to deal with this type of coding more and more. And for some reason I like it, which is much simpler than Qt and C ++ =). When I discovered such things as code generation, the Yii framework, Twitter Bootstrap and many other nishtyakov, I realized that projects for the web can fly out from the pen of a more or less experienced developer like hotcakes - three pieces a month.

So, the discussions about the constant shortage of qualified personnel that are in full swing in the next topics are stupid.

Modern development tools are so simple that 90% of the work can be done without ever knowing the difference between MySql indexes like BTree and hash. And the remaining 10% can always peek somewhere.

Well, okay, all this lyrics.

What else would you like to note


It is very easy to extend this module - it is enough to add another controller for each controller, model, form description and view. And you can get by with less effort - the new action, the descriptive form and model. You can use the same view. And then you will have two or three or ten settings pages that are independent of each other.

For me, by the way, unification and optimization is generally a disease, so I would certainly like to tie in to all this an interface for editing the settings form fields right in the browser, but this is already in some other project.

In general, after a year of working with Yii, I have accumulated a whole box of all sorts of examples, architectural solutions and perversions, I’m going to describe them humanly, but as usual time, time ...

Look like that's it. Questions? Suggestions?

Source: https://habr.com/ru/post/172737/


All Articles