Skip to content

Latest commit

History

History
119 lines (76 loc) 路 4.82 KB

migration-3-0.texy

File metadata and controls

119 lines (76 loc) 路 4.82 KB
Migrating to Version 3.0 ************************ Minimum required PHP version is 7.1. Always update gradually, so not from Nette 2.3 to 3.0, but first to 2.4 and then to 3.0. Before testing we recommend to disable reporting `E_USER_DEPRECATED` and allow it when everything will work: ```php $configurator->enableDebugger(); error_reporting(~E_USER_DEPRECATED); // note ~ before E_USER_DEPRECATED ``` PHP 7.1 type hints ------------------ Nette 3.0 uses typehints for parameters and return values of methods. If you inherit from a Nette class and override any of the methods that are now typehinted, PHP throws an error similar to this: ``` Fatal error: Declaration of Nette\Application\UI\Component::attached($presenter) must be compatible with Nette\ComponentModel\Component::attached(Nette\ComponentModel\IComponent $obj): void ``` It is necessary to add the same typehints to the rewritten method. You can use the tool to automatically add missing typehints - "Nette TypeFixer":https://github.com/nette/type-fixer. Simply call in the project directory (where the `/vendor` and `/app` folders are): ``` typefixer --fix my-project-dir ``` Of course, back up everything first. Forms ----- All form elements are optional by default now. This change was introduced in Nette 2.4. Now you can remove `setRequired(false)`. Be sure to update `netteForms.js` to version 3! You can install it with npm: ``` npm install nette-forms ``` The `ChoiceControl::$checkAllowedValues` and `MultiChoiceControl::$checkAllowedValues` has been replaced with method `checkDefaultValue()`. Presenters & Components ----------------------- Constructor of `Nette\ComponentModel\Component` has not been used for years and was removed in version 3.0. It's a BC break. If you call parent constructor in your component or presenter inheriting from `Nette\Application\UI\Presenter`, you must remove it. Interface `Nette\Application\IRouter` has been changed, see "old":https://github.com/nette/application/blob/v2.4.0/src/Application/IRouter.php and "new":https://github.com/nette/routing/blob/v3.0.0/src/Routing/Router.php. Now method `match()` returns and `constructUrl()` accepts array of parameters instead of object `Nette\Application\Request`. Nette now checks if each "signal":https://doc.nette.org/en/3.0/components#toc-signal-subrequest is sent from the same origin (ie. from the same domain and subdomain). The same-origin policy is a critical security mechanism that helps reduce possible attack vectors. If you want to allow another origins, add the annotation `@crossOrigin` to the handle method: ```php /** * @crossOrigin */ function handleXy() { } ``` This also applies to the submission of forms. If you want to allow submission from another origins, do it this way: ```php $form = new Nette\Application\UI\Form; $form->allowCrossOrigin(); ``` Dependency Injection -------------------- Support for INI files has been removed. Direct writing of PHP code to the configuration using question marks was removed. E.g: ```neon setup: - "$service->onError[] = ?"([@Some\Logger, logApplicationError]) ``` can be replaced by: ```neon setup: - '$service->onError[]' = [@Some\Logger, logApplicationError] ``` In your configuration files, you should use `factory: PDO(...)` instead of `class: PDO(...)`. Tag `nette.presenter` is not used for presenters anymore. DI for Compiler Extensions Autors --------------------------------- While Nette 2.4 internally described each service as `Nette\DI\ServiceDefinition`, there are several definitions today, such as `Nette\DI\Definitions\ImportedDefinition` for imported or dynamic services, `Nette\DI\Definitions\FactoryDefinition` for generated inteface-based factories, `Nette\DI\Definitions\AccessorDefinition` for generated accessors, and `Nette\DI\Definitions\ServiceDefinition` for common services. So there are several other methods to create a new definition in addition to `ContainerBuilder::addDefinition()`: `addFactoryDefinition()`, `addAccessorDefinition()`, `addImportedDefinition()`. Others ------ The `Nette\Security\Passwords` class is now used as an object, ie the methods are no longer static. Some methods, especially from Nette Database, such as `fetch()` or `fetchField()`, return NULL instead of FALSE in case of an error (ie when there is no other row). Interface `Nette\Localization\ITranslator` has been changed, see "old":https://github.com/nette/utils/blob/v2.5.0/src/Utils/ITranslator.php and "new":https://github.com/nette/utils/blob/v3.0.0/src/Utils/ITranslator.php. The `Nette\Http\UrlScript` object, which is retured for example by `Nette\Http\Request::getUrl()`, is now immutable. Nette\Object is deprecated since Nette 2.4 and removed from Nette 3.0, however it still exists under new name `Nette\LegacyObject` (name `object` cannot be used in PHP 7.1) in "nette/deprecated":https://github.com/nette/deprecated package.