6

In SilverStripe 3.4.1 I want to control _config.php file values through the Admin settings. I want to set some variables in Admin > Settings > MyTab and then access these variables in _config.php.

Is there a way to access SiteConfig variables in the _config.php file?

I tried to use several different ways to get the config data:

  • Config::inst()
    • print_r/var_dump gives all the values as an array
  • Config::inst()->get($this->class, 'PropertyName') or $this->config()-> get('PropertyName')
    • Returns empty
  • SiteConfig::current_site_config() or any other similar variations based on the previous two
    • Internal Server Error

The reason I want to do this is I have a plugin that replaces some SilverStripe default action but it requires some data to be inserted. If this data is not inserted it should stay as default.

Here are some resources I have read through to try to find a solution:

3dgoo
  • 15,716
  • 6
  • 46
  • 58
lohe
  • 109
  • 11
  • **For more I used**: [link](https://docs.silverstripe.org/en/3.4/developer_guides/configuration/configuration/) **AND** [link](https://docs.silverstripe.org/en/3.4/developer_guides/configuration/siteconfig/) – lohe Aug 31 '16 at 16:47

1 Answers1

6

The issue is DB::connect is not called at that stage in _config.php. Therefore we cannot retrieve items from the database.

What we can do is call DB::connect in our _config.php before we retrieve our SiteConfig.

Assuming our database config settings are stored in $databaseConfig, here is the code to fetch our SiteConfig:

DB::connect($databaseConfig);
$siteConfig = SiteConfig::current_site_config();

Then we can retrieve SiteConfig variables like so:

$siteConfig->Title;
3dgoo
  • 15,716
  • 6
  • 46
  • 58
  • There is one more thing to add. IF to use it, remember that cause DB connection is opened earlier, the adding/removing DB fields (at least for setting page, I tried) will end up with error 500. To avoid that, that earlier db:connect must be disabled. – lohe Sep 01 '16 at 11:24