Yii 1.1: Setting application parameters dynamically in the back-end


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.

How to do it

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();
        if (isset($_POST['ConfigForm']))
            $config = array(        
            $str = base64_encode(serialize($config));
            file_put_contents($file, $str);
            Yii::app()->user->setFlash('config', Yii::t('app', 'Your new options have been saved.'));

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'); ?>
    <?php endif; ?>
    <p class="note">
        <?php echo Yii::t('app', 'Fields with'); ?> <span class="required">*</span> <?php echo Yii::t('app', 'are required'); ?>.
    <?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')); ?>
<?php $this->endWidget(); ?>
</div><!-- form -->

Now, we will create the following config files under protected/config:

  • params.inc
  • params.php

We need to change our protected/config/main.php as follows:

// application-level parameters that can be accessed
// using Yii::app()->params['paramName']

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(

How it works

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

#16893 report it
susahoo at 2014/04/10 05:48am

hi all,

how to run this in browser and how to create params.inc. please help me as i am new in yii

#14878 report it
migueArgentina at 2013/09/17 09:35pm

Thank you very much, man!

I was looking for some easy solution for my control panel problem.

#12996 report it
kicoe at 2013/04/27 04:34am
Completing doodle's sharing


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 :

$app = Yii::createWebApplication($config);
$myparamtable = Myparammodel::model()->find();
foreach( $myparamtable->attributes as $key => $value )
    $app->params->add($key, $value);

And then I just have to call this in the application :


I love simple solutions, you provided me one, so thank you :)

#10820 report it
sebako at 2012/11/26 06:36am
really useful

thank you! this has been really useful for me!

#7895 report it
Vojtech at 2012/04/25 05:39am
@got 2 doodle

Thanks! This is what I was looking for.

#6974 report it
got 2 doodle at 2012/02/16 10:42am
Just sharing

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.

// create the application instance but don't run it yet
$app = Yii::createWebApplication($config);
// see if a conference id is present otherwise we want the default conference (active, most recent)
    $app->params->conferenceID = $_GET['c'];
} else {
    $app->params->conferenceID = Conference::activeConference();
//  here we go!

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 to leave your comment.

Write new article