How to get the config params


Dec '14

Aug '16

5

2278

1


Thien
31.3k
edited Jul '15
Dec '14

I assume have config file look like

/app/config/config.php
return new \Phalcon\Config(array(
    'database' => array(
        'adapter'  => 'Mysql',
        'host'     => '127.0.0.1',
        'username' => 'root',
        'password' => 'root',
        'dbname'   => 'taobao',
        'prefix'   => 'sh_',
    ),
));

Then you want to call it in model, controller or volt. You must following step here:

<?php
//in services.php
$di->set('config', function () use ($config) {
    return $config;
}, true);

//in controller
$this->config->database->dbname;

//in model
$this->getDI()->get('config')->database->prefix
//$this->getDI()->getConfig()

//In view
{{this.config.database.prefix }}

Boom :)


tmihalik
2.2k

easier:

<?php
//in services.php
$di->set('config', $config, true);

Thank you very much, I have used phalcon devtools to create simple project and I have got lost and nearly becomes MAD for just how to access those configs. You simply show me how is it simple.


Thien
13.1k

@Said You are wellcome on Phalcon Tips :)

And how in a helper-class?