Configuring DI Container
Overview of configuration options for the Nette DI container.
Nette DI container is easy to control using configuration files. They are usually written in NEON format. We recommend to use editors with support for this format for editing.
decorator: Decorator
di: DI Container
extensions: Install additional DI extensions
includes: Including files
parameters: Parameters
services: Services
If you use a string that starts with @
or has %
anywhere in it, you need to escape it by
adding another @
or %
.
Parameters
You can define parameters that can then be used as part of service definitions. This can help to separate out values that you will want to change more regularly.
parameters:
dsn: 'mysql:host=127.0.0.1;dbname=test'
user: root
password: secret
You can refer to foo
parameter via %foo%
elsewhere in any config file. They can also be used inside
strings like '%wwwDir%/images'
.
Parameters do not need to be just strings, they can also be array values:
parameters:
mailer:
host: smtp.example.com
secure: ssl
user: franta@gmail.com
languages: [cs, en, de]
You can refer to single key as %mailer.user%
.
If you need to get the value of any parameter in your code, for example in your class, then pass it to this class. For example, in the constructor. There is no global configuration object which can classes query for parameter values. This would be against to the principle of dependency injection.
Services
See separate chapter.
Decorator
How to bulk edit all services of a certain type? Need to call a certain method for all presenters inheriting from a particular common ancestor? That's where the decorator comes from.
decorator:
# for all services that are instances of this class or interface
App\Presenters\BasePresenter:
setup:
- setProjectId(10) # call this method
- $absoluteUrls = true # and set the variable
Decorator can also be used to set tags or turn on inject mode.
decorator:
InjectableInterface:
tags: [mytag: 1]
inject: true
DI
Technical settings of the DI container.
di:
# shows DIC in Tracy Bar?
debugger: ... # (bool) defaults to true
# parameter types that you never autowire
excluded: ... # (string[])
# the class from which the DI container inherits
parentClass: ... # (string) defaults to Nette\DI\Container
Extensions
Registration of other DI extensions. In this way we add, for example, DI extension
Dibi\Bridges\Nette\DibiExtension22
under the name dibi
:
extensions:
dibi: Dibi\Bridges\Nette\DibiExtension22
Then we configure it in it's section called also dibi
:
dibi:
host: localhost
You can also add a extension class with parameters:
extensions:
application: Nette\Bridges\ApplicationDI\ApplicationExtension(%debugMode%, %appDir%, %tempDir%/cache)
Including Files
Additional configuration files can be inserted in the includes
section:
includes:
- parameters.php
- services.neon
- presenters.neon
The name parameters.php
is not a typo, the configuration can also be written in a PHP file, which returns it as
an array:
<?php
return [
'database' => [
'main' => [
'dsn' => 'sqlite::memory:',
],
],
];
If items with the same keys appear within configuration files, they will be overwritten or merged in
the case of arrays. Later included file has a higher priority than the previous one. The file in which the includes
section is listed has a higher priority than the files included in it.
Merging
If items with the same keys appear in more configuration files, they will be overwritten or merged in the case of arrays. The later included file has a higher priority.
config1.neon | config2.neon | result |
---|---|---|
|
|
|
To prevent merging of a certain array use exclamation mark right after the name of the array:
config1.neon | config2.neon | result |
---|---|---|
|
|
|