I was wondering how to set-up the application parameters in the back-end to use them all around the application without the need of using the database and I came up with this solution, I hope it helps somebody else.
Note: The directory organization described follows this tutorial.
We will start with the model, so create protected/backend/models/ConfigForm.php as follows:
class ConfigForm extends CFormModel { public $adminEmail; public $paramName; public function rules() { return array( array('adminEmail, paramName','required'), ); } }
Now we will move on to the controller, so create protected/backend/controllers/ConfigController.php as follows:
class ConfigController extends Controller { public function actionIndex() { $file = dirname(__FILE__).'../../../config/params.inc'; $content = file_get_contents($file); $arr = unserialize(base64_decode($content)); $model = new ConfigForm(); $model->setAttributes($arr); if (isset($_POST['ConfigForm'])) { $config = array( 'adminEmail'=>$_POST['ConfigForm']['adminEmail'], 'paramName'=>$_POST['ConfigForm']['paramName'], ); $str = base64_encode(serialize($config)); file_put_contents($file, $str); Yii::app()->user->setFlash('config', Yii::t('app', 'Your new options have been saved.')); $model->setAttributes($config); } $this->render('index',array('model'=>$model)); } }
Finally a view protected/backend/views/config/index.php
<div class="form"> <?php $form = $this->beginWidget('CActiveForm', array( 'id' => 'config-form', 'enableAjaxValidation' => false, )); <h1><?php echo Yii::t('app', 'Options'); </h1> <?php if(Yii::app()->user->hasFlash('config')): <div class="info"> <?php echo Yii::app()->user->getFlash('config'); </div> <?php endif; <p class="note"> <?php echo Yii::t('app', 'Fields with'); <span class="required">*</span> <?php echo Yii::t('app', 'are required'); . </p> <?php echo $form->errorSummary($model); <div class="row"> <?php echo $form->labelEx($model,'adminEmail'); <?php echo $form->textField($model, 'adminEmail'); <?php echo $form->error($model,'adminEmail'); </div><!-- row --> <div class="row"> <?php echo $form->labelEx($model,'paramName'); <?php echo $form->textField($model, 'paramName'); <?php echo $form->error($model,'paramName'); </div><!-- row --> <div class="row buttons"> <?php echo CHtml::submitButton(Yii::t('app', 'Save')); </div> <?php $this->endWidget(); </div><!-- form -->
Now, we will create the following config files under protected/config:
We need to change our protected/config/main.php as follows:
// application-level parameters that can be accessed // using Yii::app()->params['paramName'] 'params'=>require(dirname(__FILE__).'/params.php'),
And protected/config/params.php will contain:
$file = dirname(__FILE__).'/params.inc'; $content = file_get_contents($file); $arr = unserialize(base64_decode($content)); return CMap::mergeArray( $arr, array( 'salt'=>'P@bl0', 'someOption'=>true, ) ) ;
You can add as many parameters as you want to the ConfigForm model. In the back-end the administrator will be able to change this parameters that will be serialized into a file and loaded into the front-end.
If you have static parameters, which doesn't need to be modified, you can add them to the protected/config/params.php file where they will be merged with all the others.
In protected/config/main.php the 'params' attribute is populated with an array of name/value pairs. These options are available at runtime via the Yii::app()->params['adminEmail'] hash, which is indexed by the parameter name.
Total 4 comments
Hi,
Thank you got 2 doodle for your sharing, very inspiring. In fact, it led me to a simple solution (5 lines) where all I could find anywher else was complex extensions... That did not work since I don't have a param table with X records of key-value items, but I have one entry withi severall columns, something like title, email, defaultimage, facebook account... etc.
So I just did that :
And then I just have to call this in the application :
I love simple solutions, you provided me one, so thank you :)
thank you! this has been really useful for me!
Thanks! This is what I was looking for.
The entry script (index.php) typically creates the Yii object like this.
I found out that you can add or change a param when the application loads like this.
Just another way to use the params.
doodle EDIT Now why would someone give thumbs down for this comment? How lame is that! How about leaving a comment if you have something to teach me?
Leave a comment
Please login to leave your comment.