Accessing $site from inside config.php

Is there a way to access $site from config.php?

I’d like to save my algolia Key, Index, App id in site.txt and then do something like:

c::set('', $site->algolia_app());
c::set('algolia.key', site()->algolia_key());
c::set('algolia.index', site()->algolia_index());

Is it possible to do something like it?

Are you in a multi-lang environment?

The last two examples above should work in single language environment.

For multi-lang, you can do something like this:

c::set('', function($site) {
  return [
    'app'   => $site->algolia_app()->value(),
    'key'   => $site->algolia_key()->value(),
    'index' => $site->algolia_index()->value()

Template/controller/plugin etc.

$settings = c::get('', []);
$settings = is_callable($settings) ? $settings(site()) : $settings;
echo $settings['app'];
echo $settings['key'];
echo $settings['index'];

Thank you for the help!

I am in a multi-language environment.

If I put the code you suggested for Template/controller/plugin into site/plugins/algolia-settings/algolia-settings.php, I get:

Parse error: syntax error, unexpected ';' in /Users/Sites/kirby/site/plugins/algolia-settings/algolia-settings.php on line 5

If I put it insted into site/controllers/site.php I get:

Method Kirby\Panel\View::__toString() must not throw an exception, caught Error: Please set your Algolia API credentials in the Kirby configuration.`

I see, the error is thrown by the algolia plugin, because the constructor expects the following settings:

public function __construct() {
    $app = c::get('');
    $key = c::get('algolia.key');
    if(!$app || !$key) throw new Error('Please set your Algolia API credentials in the Kirby configuration.');
    $this->algolia = new AlgoliaClient($app, $key);

So what I suggested above won’t work unless you modify the algolia plugin to work with the new settings.

As regards the error thrown in line 5, sorry, I forgot to remove the opening parenthesis when I changed from dump to echo…