Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access Phalcon configuration data in an external library?

Tags:

php

phalcon

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?

like image 711
Ayush Sharma Avatar asked Feb 21 '16 06:02

Ayush Sharma


2 Answers

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.

like image 161
Scriptonomy Avatar answered Oct 14 '22 02:10

Scriptonomy


in your controller class, call config by

$this->config
like image 2
Fazal Rasel Avatar answered Oct 14 '22 04:10

Fazal Rasel