Within my project, I've created a "core" directory which contains certain classes and methods called throughout the controllers. I've defined a configuration parameters in my bootstrap file like so:
private function loadConfig ()
{
// Bootstrap
$configFile = __DIR__ . '/../config/config.json';
// Create the new object
$config = json_decode ( file_get_contents ( $configFile ) );
// Store it in the Di container
$this->di->setShared ( 'config', $config );
}
I want to be able to access these configuration values in my "core" classes.
What do I do?
There are several ways to get a reference to the service you registered with the Dependency Injector. However, to make sure you are getting the same instance of the service and not a newly generated one, then you need to use the getShared method:
$this->getDI()->getShared('config');
Doing so ensures you are getting the highest performance possible, and minimizing memory footprint.
in your controller class, call config by
$this->config
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With