Question

I'm trying to build a custom extension in Yii, but having trouble accessing the custom configuration.

Suppose I have the following in main.php:

'my_extension'=>array(
    'class'=>'ext.my_extension.my_extension'
    'custom_config'=>array(
        'first_option'=>array(
            'active'=>true,
            'custom_username'=>'username',
            'custom_password'=>'password',
        ),
    ),
),

How do I get access to 'active', 'custom_username' and 'custom_password'?

In my extension, which extends CFormModel, I tried:

Yii::app()->my_extension->custom_config['first_option']['custom_username'];

But I get the error:

Property "my_extension.custom_config" is not defined.
Was it helpful?

Solution

the reason this is erroring is because you are trying to access a property of Yii::app(), which is not declared, defined in a array..

The entry script (index.php) typically creates the Yii object like this..

require_once($yii);
Yii::createWebApplication($config)->run();

the $config variable is your main.config file array, so even if you add a index in array, which doesn't match in the properties of Yii::app(), it won't create that..

To do set custom configs, Yii provides us params..which can be used as Yii::app()->params['paramName']

so in you case it'll be, in the end of config.main the 'params' index will be like..

'params'=>array(
    'my_extension'=>array(
        'class'=>'ext.my_extension.my_extension',
        'custom_config'=>array(
            'first_option'=>array(
                'active'=>true,
                'custom_username'=>'username',
                'custom_password'=>'password',
            ),
        ),
    ),
    //...remaining params
),

and the usage will be

Yii::app()->params['my_extension']['custom_config']['first_option']['custom_username'];

Edit:

There are cases when u need to configs of yr extension, or some thing like facebook or postmark separately, maybe because of having separate values in separate environments, and want to put that file in .gitignore, or maybe because you want to release yr extension to open-source, and want people to give clear way of having its own config..

So in those cases, the solution is make a file in yr ext, or anywhere, make a array in that which contains the config values, include that file in index.php, and in yr config/main, put that content..

In your case:

//in index.php
require_once(dirname(__FILE__).'/protected/ext/yr_ext/ext_config.php');

//in ext/yr_ext/ext_config.php
<?php
class ExtConfiguration {
public static function fetchConfigArray() {
        return array(
        'class'=>'ext.my_extension.my_extension',
                'custom_config'=>array(
                    'first_option'=>array(
                        'active'=>true,
                        'custom_username'=>'username',
                        'custom_password'=>'password',
                    ),
                ),
            );
    }
}
?>

//in config/main.php
//on top before array start
$ext_config = ExtConfiguration::fetchConfigArray();

//in params
'params'=>array(
    'my_extension'=>$ext_config,
),
//...remaining params

OTHER TIPS

By the config fragment you shared us, you mentioned you must define in your main class the public property $custom_config.

And simply you use like in your extension like $this->custom_config

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top