Easiest way to access Phalcon config values in views?

12,132

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 }}
Share:
12,132
meder omuraliev
Author by

meder omuraliev

I began learning front end web development around 04-05, started my first program around 06 with PHP. Currently, I am a Web technologist specializing in full stack development and linux administration specializing with the LAMP stack ( HTML5, CSS3, PHP, Apache, MySQL). I also like to dabble in node.js, meteorjs, Python, django and in general like to mess with new technology/stacks. LinkedIn | [email protected]

Updated on June 09, 2022

Comments

  • meder omuraliev
    meder omuraliev almost 2 years

    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?