You are browsing the unmaintained documentation for old Nette 2.2. See documentation for current Nette.

Bootstrap

Bootstrap is boot code that initializes the environment, creates a dependency injection (DI) container, and starts the application. We will discuss:

  • create the system container
  • configure your application using NEON files
  • handle production and development modes
  • create and use your own container extensions

System container is a static Dependency Injection container, that holds all services and parameters the application needs. This means not only services of the framework itself, but of each library you have decided to use. Take a look at an example of such system container.

Developers learned that writing containers is a very tedious work. And it surely gets difficult to manage the application as it grows. Nette will do the work for you! With a simple configuration language we will describe services of the application and the framework will generate the PHP code. In all seriousness – the container linked above was generated this way as well.

Configurator

The task of code generation is given to Nette\Configurator class. The compilation process is triggered only once and its result is cached. For that reason we need to choose a directory for storing this code.

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

Now we just add a path to the config file:

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

And getting container's instance is the simplest one:

// returns SystemContainer instance
$container = $configurator->createContainer();

Environment

Configurator attempts to detect if the application is running on production or development server. As long as the client's IP address is 127.0.0.1, server will be considered as developmental. We cannot find out which environment is used, but it doesn't care since it is used only for loading section from the configuration file. We can define it by a second parameter of addConfig().

$environment = Nette\Configurator::detectDebugMode('your ip address')
	? $configurator::DEVELOPMENT
	: $configurator::PRODUCTION;

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

We can pass any string by $environment, not only the production or development. By this, we can ask Configurator to load any section of configuration file. But a right detection is up to you (for example by system variable getenv('COMPUTERNAME') or server's hostname $_SERVER['SERVER_NAME']).

Development mode

A little bit different thing is an application running mode. We can access a production server as a developer and we want activate a Tracy debugger. The running mode is distinguished by the same principle as environment above, by client's IP address. We get the mode by isDebugMode() and set the mode by setDebugMode(). It is useful when we enable debugger by enableDebugger() which must be placed after the mode setting.

// activates Tracy debugger for listed IP adresses
$configurator->setDebugMode(array('90.180.45.360', '90.180.45.361'));
// or for anyone
$configurator->setDebugMode(); // = TRUE
// or for no one
$configurator->setDebugMode($configurator::NONE); // = FALSE

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

Class RobotLoader

To compile the container, Configurator needs to load all the classes mentioned in a configuration files. It is handy to have auto-loading at your disposal and Configurator offers a method creating an instance of RobotLoader. We add only directories we wish to be indexed and activate the loader. Do not forget to place this code before calling createContainer().

$configurator->createRobotLoader()
	->addDirectory(__DIR__)
	->addDirectory(__DIR__ . '/../lib')
	->register();