Skip to content

Commit

Permalink
Add section about dependency injection of Phalcon\Config
Browse files Browse the repository at this point in the history
Fix typos error

Fix typos
  • Loading branch information
zamronypj committed Jul 20, 2016
1 parent 77ec3cd commit 5399104
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions en/reference/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,38 @@ The above code produces the following:
)

There are more adapters available for this components in the `Phalcon Incubator <https://github.com/phalcon/incubator>`_

Injecting Configuration Dependency
----------------------------------
You can inject configuration dependency to controller allowing us to use :doc:`Phalcon\\Config <../api/Phalcon_Config>` inside :doc:`Phalcon\\Controller <../api/Phalcon_Controller>`. To be able to do that, add following code inside your dependency injector script.

.. code-block:: php
<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Config;
// Create a DI
$di = new FactoryDefault();
$di->set('config', function () {
$configData = require 'config/config.php';
return new Config($configData);
});
Now in your controller you can access your configuration by using dependency injection feature using name `config` like following code:

.. code-block:: php
<?php
use Phalcon\Mvc\Controller;
class MyController extends Controller
{
private function getDatabaseName() {
return $this->config->database->dbname;
}
}

0 comments on commit 5399104

Please sign in to comment.