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

As part of my project, I created a "main" directory that contains specific classes and methods, called in all controllers. I defined the configuration options in my bootstrap file as follows:

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 have access to these configuration values โ€‹โ€‹in my โ€œcoreโ€ classes.

What should I do?

+5
source share
3 answers

There are several ways to get a link to a service registered in the Dependency Injector. However, to make sure that you are getting the same instance of the service, not just the generated one, you need to use the getShared method:

 $this->getDI()->getShared('config'); 

This ensures that you get maximum performance and minimize memory footprint.

+5
source

in your controller class, call config

 $this->config 
+2
source

You can access services from any classes that implement Phalcon\Di\Injectable

  • Phalcon\Mvc\Controller
  • Phalcon\Mvc\User\Component
  • Phalcon\Mvc\User\Module
  • Phalcon\Mvc\User\Plugin
  • etc.

Examples:

 $this->getDI()->get('config'); // The same as $this->config $this->getDI()->getShared('config'); 
+1
source

Source: https://habr.com/ru/post/1243487/


All Articles