Nette Documentation Preview

syntax
Configuring Application
***********************

.[perex]
Overview of configuration options for the Nette Application.


Application
===========

```neon
application:
	# shows "Nette Application" panel in Tracy BlueScreen?
	debugger: ...           # (bool) defaults to true

	# will error-presenter be called on error?
	# has effect only in developer mode
	catchExceptions: ...    # (true|false|4xx) defaults to 4xx

	# name of error-presenter
	errorPresenter: Error   # (string|array) defaults to 'Nette:Error'

	# defines the rules for resolving the presenter name to a class
	mapping: ...

	# do bad links generate warnings?
	# has effect only in developer mode
	silentLinks: ...        # (bool) defaults to false
```

As of `nette/application` version 3.2, the default value for `catchExceptions` is `4xx`, which means that the error-presenter is used only for `Nette\Application\BadRequestException` exceptions representing HTTP errors ''4xx''.
Furthermore, it is possible to define a pair of error-presenters:

```neon
application:
	errorPresenter:
		4xx: Error4xx   # for Nette\Application\BadRequestException
		5xx: Error5xx   # for other exceptions
```

Option `silentLinks` determines how Nette behaves in developer mode when link generation fails (for example, because there is no presenter, etc). The default value `false` means that Nette triggers `E_USER_WARNING`. Setting to `true` suppresses this error message. In a production environment, `E_USER_WARNING` is always invoked. We can also influence this behavior by setting the presenter variable [$invalidLinkMode |creating-links#Invalid Links].

The [mapping defines the rules |modules#mapping] by which the class name is derived from the presenter name.


Automatic Registration of Presenters
------------------------------------

Nette automatically adds presenters as services to the DI container, which significantly speeds up their creation. How Nette finds out presenters can be configured:

```neon
application:
	# to look for presenters in Composer class map?
	scanComposer: ...      # (bool) defaults to true

	# a mask that must match the class and file name
	scanFilter: ...        # (string) defaults to '*Presenter'

	# in which directories to look for presenters?
	scanDirs:              # (string[]|false) defaults to '%appDir%'
		- %vendorDir%/mymodule
```

The directories listed in `scanDirs` do not override the default value `%appDir%`, but complement it, so `scanDirs` will contain both paths `%appDir%` and `%vendorDir%/mymodule`. If we want to overwrite the default directory, we use [exclamation mark |dependency-injection:configuration#Merging]:

```neon
application:
	scanDirs!:
		- %vendorDir%/mymodule
```

Directory scanning can be turned off by setting false. We do not recommend completely suppressing the automatic addition of presenters, otherwise application performance will be reduced.


Latte
=====

This setting globally affects the behavior of Latte in components and presenters.

```neon
latte:
	# shows Latte panel in the Tracy Bar for the main template (true) or for all components (all)?
	debugger: ...        # (true|false|'all') defaults to true

	# switches Latte to XHTML mode (deprecated)
	xhtml: ...           # (bool) defaults to false

	# generates templates with declare(strict_types=1)
	strictTypes: ...     # (bool) defaults to false

	# class of $this->template
	templateClass: App\MyTemplateClass # defaults to Nette\Bridges\ApplicationLatte\DefaultTemplate
	# resp. Nette\Bridges\ApplicationLatte\Template in Nette Application v3.0
```

If you are using Latte version 3, you can add new [extension |latte:creating-extension] using:

```neon
latte:
	extensions:
		- Latte\Essential\TranslatorExtension
```

If you are using Latte version 2, you can register new tags either by entering the class name or by referring to the service. Method `install()` is called by default, but this can be changed by specifying the name of another method:

```neon
latte:
	# registration of user Latte tags
	macros:
		- App\MyLatteMacros::register         # static method, classname or callable
		- @App\MyLatteMacrosFactory           # service with install method
		- @App\MyLatteMacrosFactory::register # service with register method

services:
	- App\MyLatteMacrosFactory
```


Routing
=======

Basic settings:

```neon
routing:
	# shows routing panel in Tracy Bar?
	debugger: ...   # (bool) defaults to true

	# to serialize router to DI container?
	cache: ...      # (bool) defaults to false
```

Router is usually defined in the [RouterFactory |routing#Route Collection] class. Alternatively, routs can also be defined in the configuration using `mask: action` pairs, but this method does not offer such a wide variation in settings:

```neon
routing:
	routes:
		'detail/<id>': Admin:Home:default
		'<presenter>/<action>': Front:Home:default
```


Constants
=========

Creating PHP constants.

```neon
constants:
	FOOBAR: 'baz'
```

The `FOOBAR` constant will created after startup.

.[note]
Constants should not serve as globally available variables. To pass values to objects, use [dependency injection |dependency-injection:passing-dependencies].


PHP
===

You can set PHP directives. An overview of all directives can be found at [php.net |https://www.php.net/manual/en/ini.list.php].

```neon
php:
	date.timezone: Europe/Prague
```


DI Services
===========

These services are added to the DI container:

| Name | Type | Description
|----------------------------------------------------------
| `application.application` | [api:Nette\Application\Application] | [full application launcher |how-it-works#Nette Application]
| `application.linkGenerator` | [api:Nette\Application\LinkGenerator] | [LinkGenerator |creating-links#LinkGenerator]
| `application.presenterFactory` | [api:Nette\Application\PresenterFactory] | presenter factory
| `application.###` | [api:Nette\Application\UI\Presenter] | individual presenters
| `latte.latteFactory` | [api:Nette\Bridges\ApplicationLatte\LatteFactory] | factory for `Latte\Engine`
| `latte.templateFactory` | [api:Nette\Application\UI\TemplateFactory] | factory for [`$this->template` |templates]

Configuring Application

Overview of configuration options for the Nette Application.

Application

application:
	# shows "Nette Application" panel in Tracy BlueScreen?
	debugger: ...           # (bool) defaults to true

	# will error-presenter be called on error?
	# has effect only in developer mode
	catchExceptions: ...    # (true|false|4xx) defaults to 4xx

	# name of error-presenter
	errorPresenter: Error   # (string|array) defaults to 'Nette:Error'

	# defines the rules for resolving the presenter name to a class
	mapping: ...

	# do bad links generate warnings?
	# has effect only in developer mode
	silentLinks: ...        # (bool) defaults to false

As of nette/application version 3.2, the default value for catchExceptions is 4xx, which means that the error-presenter is used only for Nette\Application\BadRequestException exceptions representing HTTP errors 4xx. Furthermore, it is possible to define a pair of error-presenters:

application:
	errorPresenter:
		4xx: Error4xx   # for Nette\Application\BadRequestException
		5xx: Error5xx   # for other exceptions

Option silentLinks determines how Nette behaves in developer mode when link generation fails (for example, because there is no presenter, etc). The default value false means that Nette triggers E_USER_WARNING. Setting to true suppresses this error message. In a production environment, E_USER_WARNING is always invoked. We can also influence this behavior by setting the presenter variable $invalidLinkMode.

The mapping defines the rules by which the class name is derived from the presenter name.

Automatic Registration of Presenters

Nette automatically adds presenters as services to the DI container, which significantly speeds up their creation. How Nette finds out presenters can be configured:

application:
	# to look for presenters in Composer class map?
	scanComposer: ...      # (bool) defaults to true

	# a mask that must match the class and file name
	scanFilter: ...        # (string) defaults to '*Presenter'

	# in which directories to look for presenters?
	scanDirs:              # (string[]|false) defaults to '%appDir%'
		- %vendorDir%/mymodule

The directories listed in scanDirs do not override the default value %appDir%, but complement it, so scanDirs will contain both paths %appDir% and %vendorDir%/mymodule. If we want to overwrite the default directory, we use exclamation mark:

application:
	scanDirs!:
		- %vendorDir%/mymodule

Directory scanning can be turned off by setting false. We do not recommend completely suppressing the automatic addition of presenters, otherwise application performance will be reduced.

Latte

This setting globally affects the behavior of Latte in components and presenters.

latte:
	# shows Latte panel in the Tracy Bar for the main template (true) or for all components (all)?
	debugger: ...        # (true|false|'all') defaults to true

	# switches Latte to XHTML mode (deprecated)
	xhtml: ...           # (bool) defaults to false

	# generates templates with declare(strict_types=1)
	strictTypes: ...     # (bool) defaults to false

	# class of $this->template
	templateClass: App\MyTemplateClass # defaults to Nette\Bridges\ApplicationLatte\DefaultTemplate
	# resp. Nette\Bridges\ApplicationLatte\Template in Nette Application v3.0

If you are using Latte version 3, you can add new extension using:

latte:
	extensions:
		- Latte\Essential\TranslatorExtension

If you are using Latte version 2, you can register new tags either by entering the class name or by referring to the service. Method install() is called by default, but this can be changed by specifying the name of another method:

latte:
	# registration of user Latte tags
	macros:
		- App\MyLatteMacros::register         # static method, classname or callable
		- @App\MyLatteMacrosFactory           # service with install method
		- @App\MyLatteMacrosFactory::register # service with register method

services:
	- App\MyLatteMacrosFactory

Routing

Basic settings:

routing:
	# shows routing panel in Tracy Bar?
	debugger: ...   # (bool) defaults to true

	# to serialize router to DI container?
	cache: ...      # (bool) defaults to false

Router is usually defined in the RouterFactory class. Alternatively, routs can also be defined in the configuration using mask: action pairs, but this method does not offer such a wide variation in settings:

routing:
	routes:
		'detail/<id>': Admin:Home:default
		'<presenter>/<action>': Front:Home:default

Constants

Creating PHP constants.

constants:
	FOOBAR: 'baz'

The FOOBAR constant will created after startup.

Constants should not serve as globally available variables. To pass values to objects, use dependency injection.

PHP

You can set PHP directives. An overview of all directives can be found at php.net.

php:
	date.timezone: Europe/Prague

DI Services

These services are added to the DI container:

Name Type Description
application.application Nette\Application\Application full application launcher
application.linkGenerator Nette\Application\LinkGenerator LinkGenerator
application.presenterFactory Nette\Application\PresenterFactory presenter factory
application.### Nette\Application\UI\Presenter individual presenters
latte.latteFactory Nette\Bridges\ApplicationLatte\LatteFactory factory for Latte\Engine
latte.templateFactory Nette\Application\UI\TemplateFactory factory for $this->template