Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to access Phalcon config values in views?

Tags:

php

phalcon

volt

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?

like image 839
meder omuraliev Avatar asked Jan 09 '14 06:01

meder omuraliev


1 Answers

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 }}
like image 131
Nikolaos Dimopoulos Avatar answered Nov 06 '22 11:11

Nikolaos Dimopoulos