Version 2.4

Bootstrap

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

Configurator

The environment settings can be configured and Dependency Injection (DI) container created by Configurator. Is used in bootstrap.php located in folder app/.

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.

$configurator->setDebugMode('secret1234@23.75.345.200');

The development mode can be deactivated by:

$configurator->setDebugMode(false);

Debugging Tool Tracy

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

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

Caching

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

$configurator = new Nette\Configurator;
$configurator->setTempDirectory(__DIR__ . '/../temp');

RobotLoader

We would like to autoload classes into application automatically using RobotLoader, so we have to register it.

$configurator->createRobotLoader()
    ->addDirectory(__DIR__)
    ->register();

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

Timezone

There is a way how to setup timezone.

$configurator->setTimeZone('Europe/Prague');

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.

The configuration files are loaded with addConfig():

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

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

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

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

includes:
    - 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

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.

$configurator->addParameters([
    '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.

$configurator->addDynamicParameters([
    '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.

$configurator->addDynamicParameters([
    '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.

services:
    myservice:
        class: App\Model\MyCustomService
        dynamic: yes

Create a new instance and insert it in bootstrap:

$configurator->addServices([
    '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.

index.php

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';
$container->getByType(Nette\Application\Application::class)->run();