We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How can I access config in a libary?

I have a library of my own and I would need to access Phalcon\Config. I just can't figure out how I can reach it. How can I access config in a library?

edited Feb '16

Damn my idea was working all the time I just had another error. I can reach it like this. Is this the way I am supposed to do it?

$config = \Phalcon\Di::getDefault()->getShared('config');

edited Feb '16

Yes, this is the correct way.

Another example in model:

$this->getDI()->getSession() 
// or 
$this->getDI()->getShared('serviceNameHere');

One more example i found in Helper class of mine:

 $di = \Phalcon\DI::getDefault();
 $url = $di['url']->get(.....);
edited Feb '16

How are you registering your config?

How are you registering your librarys?

Really a lot of this question depends on your bootstrapping method with phalcon.

My advice components. https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_User_Component.html#methods

Registeer your namespaces

$loader = new \Phalcon\Loader();
$loader->registerNamespaces(array(
    'Name\Library' => $config->application->libraryDir
));
namespace Name\Library\YourClass;
use Phalcon\Mvc\User\Component;

class YourClass extends Component
{
    $this->config

    //or
    $this->getDi()->getConfig();
    // assuming you ahve registered your config file in DI() shown below. 
}
$di->set('config', $config);

there are serveral missing parts to this that i assuem you arleady have in place given the nature of your question.