Component params from database or external file

I have a component that has a variable set in the config, given that for each installation I will have to customize it, is it possible to set the parameter to be loaded from the database or from an external configuration file?
thanks

Sure. You form the config yourself. Check index.php.

1 Like
'components' => [
'foo' => [
            'class' => 'vendorName\foo\Bar',
            'token' => '609577112:xxGFoKGxZhkoAOvZyAhmeibHjPGyqX4dY-Y',
        ],
]

a more practical example for move ‘token’ param in other place, for customize it every installation ? Thanks

If this is a setting that changes with the environment (e.g. development/staging/production), then you might want to make it injected from the environment - in your config file, grab the value from an environment variable. E.g.:

'components' => [
    'foo' => [
        'class' => 'vendorName\foo\Bar',
        'token' => (strlen($_ENV['MY_APP_TOKEN']) ? $_ENV['MY_APP_TOKEN'] : 'someDefaultValue'),
    ],
]
1 Like

Where I can set a $_ENV variable ?

That’s dependant on your operating system, it’s something you set outside of Yii. You might also set it in your web server/vhost configuration. Look it up :slight_smile:

can I push into index.php ? After the ‘php init’ command does it overwrite my changes?