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:
<?php
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:
<?php
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
:
- 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']
'params'=>require(dirname(__FILE__).'/params.php'),
And protected/config/params.php
will contain:
<?php
$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,
)
)
;
?>
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.
Just sharing
The entry script (index.php) typically creates the Yii object like this.
require_once($yii); Yii::createWebApplication($config)->run();
I found out that you can add or change a param when the application loads like this.
require_once($yii); // 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) if(isset($_GET['c'])) { $app->params->conferenceID = $_GET['c']; } else { $app->params->conferenceID = Conference::activeConference(); } // here we go! $app->run();
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?
@got 2 doodle
Thanks! This is what I was looking for.
really useful
thank you! this has been really useful for me!
Completing doodle's sharing
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 :
$app = Yii::createWebApplication($config); $myparamtable = Myparammodel::model()->find(); foreach( $myparamtable->attributes as $key => $value ) { $app->params->add($key, $value); } $app->run();
And then I just have to call this in the application :
I love simple solutions, you provided me one, so thank you :)
THANKS!
Thank you very much, man!
I was looking for some easy solution for my control panel problem.
doubt
hi all,
how to run this in browser and how to create params.inc. please help me as i am new in yii
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.