Version 2.4


Nette application is easily configured using NEON files. We'll talk about:

  • how to configure your application using NEON files
  • how to handle production and development modes
  • how to create the DI container

Setting environment and creating a Dependency Injection (DI) container is in Nette in charge of the Configurator class. It is used in the bootstrap file bootstrap.php located in the app/ folder.

Development Mode

Development mode is automatically active when we develop on localhost. If you want to enable it even outside of it, for example, programmers accessing a specific IP address, we will use setDebugMode():

$configurator->setDebugMode('23.75.345.200'); // one or more IP addresses

Even more secured is the combination of IP address and cookie. We will store a secret token into the nette-debug' cookie, e.q `secret1234, the development mode is activated for programmers with this IP and set up cookie.


The development mode can be deactivated by:


Debugging Tool Tracy

Turn on great debugging tool Tracy, it'll store all exceptions files into specified folder (__DIR__ . '/../log').

$configurator->enableTracy(__DIR__ . '/../log');


The configuration is loaded and processed only once, and the result is cached, which greatly accelerates the application. Therefore, we set the setTempDirectory() path where the cache will be saved.

$configurator->setTempDirectory(__DIR__ . '/../temp');


Usually we will want to automatically load the classes using RobotLoader, so we have to start it and let it load the classes from the directory where bootstrap.php is located and all subdirectories (ie __DIR__):


Alternative way is to use only Composer PSR-4 autoloading.


There is a way how to setup timezone.


Configuration Files

According to configuration files, a system DI container is generated, which is the heart of the entire application. Typically, they are written in NEON format. In a separate chapter, you can read everything about configuration.

We load the configuration files using addConfig():

$configurator->addConfig(__DIR__ . '/config/config.neon');

Method addConfig() can be called multiple times to add multiple files.

$configurator->addConfig(__DIR__ . '/config/common.neon');
$configurator->addConfig(__DIR__ . '/config/local.neon');
if (PHP_SAPI === 'cli') {
    $configurator->addConfig(__DIR__ . '/config/cli.neon');

Alternatively we can use includes section to load more configuration files.

    - parameters.php
    - services.neon
    - presenters.neon

If items with the same keys appear in configuration files, they will be overwritten or merged in the case of arrays. The later included file has higher priority. The file with the includes section has higher priority than included files.


Parameters could be passed to the DI container over addParameters() method, their values must be known at the compile time. The container is always generated for the different values of these parameters.

    'foo' => 'bar',
    'baz' => '%tempDir%',

We can access foo parameter via %foo% in configuration files. The configurator adds be default these parameters appDir, wwwDir, tempDir, debugMode a consoleMode.

Dynamic Parameters

We can set also dynamic parameters to DI container, their values must not be known at compile time, they are used at run time. It doesn't generate a new container.

    'remoteIp' => $_SERVER['REMOTE_ADDR'],

The environment variables could be easily set up using dynamic parameters. We can access them via %env.variable% in configuration files.

    'env' => $_ENV,

Dynamic Services

We can insert an instance of the class into the DI container directly, before the DI container creates it for us. The service must be defined with dynamic: yes attribute.

        class: App\Model\MyCustomService
        dynamic: yes

Create a new instance and insert it in bootstrap:

    'myservice' => new App\Model\MyCustomService('foobar'),

Creating a Container

The container class generates and its instance returns createContainer():

/** @var Nette\DI\Container $container */
$container = $configurator->createContainer();

In the developer mode, the container is automatically updated each time you change the code or configuration files. In production mode it is generated only once and file changes are not checked for performance.


If we are creating a web application, all requests goes through one file located in public directory www/ and that is index.php. It only passes the control to the application (that is the app/ directory), to a boot file bootstrap.php. Subsequently, index.php retrieves from the DI container the service that runs the web application:

$container = require __DIR__ . '/../app/bootstrap.php';