Easiest way to access Phalcon config values in views?

12.4k views Asked by At

I have a section in ini files with some globally used social links, for ex:

[social]
fb = URL
twitter = URL
linkedin = URL

What's the easiest way to access these, or is there a better way to organize these global variables?

1

There are 1 answers

0
Nikolaos Dimopoulos On BEST ANSWER

If you read your config file upon initialization/bootstraping your application AND store it in the DI container, then it will be accessible through that in every part of your app.

Example - bootstrap

$di         = new \Phalcon\DI\FactoryDefault();
$configFile = ROOT_PATH . '/app/var/config/config.ini';

// Create the new object
$config = new \Phalcon\Config\Adapter\Ini($configFile);

// Store it in the Di container
$di->set('config', $config);

Now you can access this in your controller as such:

echo $this->config->social->twitter;

Views via Volt:

{{ config.social.twitter }}

You can always set that particular part of your config in your views through a base controller.

class ControllerBase() 
{
    public function initialize()
    {
        parent::initialize();

        $this->view->setVar('social', $this->config->social);
    }
}

and then access that variable through your view:

{{ social.twitter }}