Reputation: 5716
I have extended CWebApplication as following to add params (Application-level parameters) from database. but as of now, i dont get anything when i try to retrieve params using
echo Yii::app()->params['adminMobileNo'];
extended code,
class QelasyApplication extends CWebApplication
{
const PARAM_CACHE = 'params';
public function setParams($value)
{
echo 'test 1';
exit;
$params = parent::getParams();
print_r($params);
echo 'test 2';
exit;
$value = Yii::app()->cache->get(self::PARAM_CACHE);
if ($value === false) {
$model = new SystemParam();
$value = $model->getParam();
print_r($value);
echo 'test 3';
exit;
Yii::app()->cache->add(self::PARAM_CACHE, $value, 1800);
}
foreach ($value as $k => $v)
$params->add($k, $v);
}
}
in the main index.php
require_once($yii);
require_once($protected.'components/QelasyApplication.php');
$application = new QelasyApplication($config);
$application->run();
and in a controller, i am trying to access the params as
Yii::app()->params['abc'] = 123;
echo Yii::app()->params['basePath'];
echo Yii::app()->params['abc'];
echo Yii::app()->params['adminMobileNo'];
// print_r(Yii::app()->getParams());
exit;
but no output it just exits.
even the setParams method isnt running, coz the echo statements inside are not displayed. what could be the reason ?
this is the output i get,
CAttributeCollection#1
(
[caseSensitive] => true
[CMap:_d] => array()
[CMap:_r] => false
[CComponent:_e] => null
[CComponent:_m] => null
)
Params.php approach
$params = Yii::app()->getParams();
$value = Yii::app()->cache->get(self::PARAM_CACHE);
if ($value === false) {
$model = new SystemParam();
$value = $model->getParam();
Yii::app()->cache->add(self::PARAM_CACHE, $value, 1800);
}
foreach ($value as $k => $v)
$params->add($k, $v);
still i am unable to get it work with Yii::app()->cache->get(self::PARAM_CACHE)
Upvotes: 2
Views: 5246
Reputation: 5716
Defining the following in main.php worked. (empty array)
'params' => array(),
This is with first approach.
Upvotes: 2
Reputation: 2815
Why don't you create params.php
in config
folder. Create your param array from db and return it. Then in your
protected/config/main.php
add
...
'params' => require('params.php'),
...
The you will be able to access them as:
Yii::app()->params['YOUR_PARAM_KEY'];
Upvotes: 1