Sharing some config value between classes

Permalink
This question has a specific and general part.

Specifically, I have a package where the package controller and a single page controller both need to know a given string value. I would rather not hard code the value in both classes and either just hard code it in the package controller or even better yet hard code it in the concrete5-cif file. I tried using \Concrete\Core\Package\Package::getConfig()->set('name', 'stringvalue') and then \Concrete\Core\Page\Controller\PageController::get(), but it didn't work. What is a good way to accomplish this?

In general, what is the best way to answer these questions myself? I tried
https://documentation.concrete5.org/api/8.5.2/... but didn't make much progress. I typically find myself including a bunch of syslog(LOG_INFO, json_encode(get_class_methods($bla))); however, feel there has to be something better.

Thanks

View Replies: View Best Answer
hutman replied on at Permalink Best Answer Reply
hutman
Here is what I have done

use \Concrete\Core\Package\Package;
$package = Package::getByHandle('package_handle');
$config = $package->getConfig();
$config->save('config_handle', 'value');
$config_value = $config->get('config_handle', null);
NotionCommotion replied on at Permalink Reply
Actually, after trying it, I don't get the expected results but an empty array. Do you know what I am doing wrong?
$package = Package::getByHandle('gb_manual');
        $config = $package->getConfig();
        $config->save('config_handle', 'value');
        $config_value = $config->get('config_handle', null);
        syslog(LOG_ERR, json_encode($config_value));    //outputs []
hutman replied on at Permalink Reply
hutman
Can you put your actual code here so we can see it? Based on what you posted you should be getting the string "value" back.
NotionCommotion replied on at Permalink Reply
Thank you, and sorry I missed your request. Full code below.

<?php
namespace Concrete\Package\GbManual;
defined('C5_EXECUTE') or die('Access Denied.');
use Concrete\Core\Package\Package;
use Concrete\Core\Asset\AssetList;
class Controller extends Package
{
    protected $pkgHandle = 'gb_manual';
    protected $appVersionRequired = '8.5.1';
    protected $pkgVersion = '0.0.1';
    public function getPackageDescription()
    {
        return t('Adds the GB manual to your website.');
    }
    public function getPackageName()
NotionCommotion replied on at Permalink Reply
Hi hutman,

While troubleshooting this approach, I found that this approach saves the data in the DB which isn't ideal if it is being used for a single hit to the server.

As an alternative, C5 uses Laravel's container perhttps://documentation.concrete5.org/developers/appendix/concrete5-ve... Then perhttps://gist.github.com/davejamesmiller/bd857d9b0ac895df7604dd2e63b2... we can do:
$this->app->instance('database.name', 'testdb');
$db_name = $this->app->make('database.name');


Initial testing is promising. Any concerns of this approach? Thanks