Version 2.4

DI: Built-in Container Extensions

Package nette/di contains a few built-in extension to DI container.

ExtensionsExtension

Using this extension we can add a more extensions defined in NEON configuration files.

extensions:
    dibi: Dibi\Bridges\Nette\DibiExtension22

That's the way how we add Dibi\Bridges\Nette\DibiExtension22 extension named dibi, so settings will be in dibi section.

dibi:
    host: localhost

We can pass arguments into constructor for extension class.

extensions:
    application: Nette\Bridges\ApplicationDI\ApplicationExtension(%debugMode%, %appDir%, %tempDir%/cache)

InjectExtension

This extension allows us to use option inject: true, which enables passing dependencies via variables with @inject annotation and inject* methods.

services:
    blog.articles:
        factory: App\Model\Articles
        inject: true
namespace App\Model;

use Nette\Database\Connection;

class Articles
{
    /** @var Connection @inject */
    public $connection;
}

By default is inject attribute enabled only for presenters.

DecoratorExtension

Using this extensions we can add tags or add setup (eg call methods) to all services of given class or implementing given interface.

decorator:
    # key is class or interface name
    App\Repository\AbstractRepository:
        setup:
            - setConnection
        inject: yes

    App\Component\BaseControl:
        setup:
            - setTranslator(@mytranslator)
        tags: [view]

PhpExtension

You can set PHP extensions using this extension. For an overview of all directives, look at php.net.

php:
    date.timezone: Europe/Prague

ConstantExtension

We can define PHP contants by this extension.

constant:
    FOOBAR: 'baz'

The FOOBAR constant will available in our application after startup.