So you have decided to upgrade to v4! Congratulations!!
Phalcon v4 contains a lot of changes to components, including changes to interfaces, strict types, removal of components and additions of new ones. This document is an effort to help you upgrade your existing Phalcon application to v4. We will outline the areas that you need to pay attention to and make necessary alterations so that your code can run as smoothly as it has been with v3. Although the changes are significant, it is more of a methodical task than a daunting one.
Phalcon v4 supports only PHP 7.2 and above. PHP 7.1 was released 2 years ago and its active support has lapsed, so we decided to follow actively supported PHP versions.
Phalcon requires the PSR extension. The extension can be downloaded and compiled from this GitHub repository. Installation instructions are available in the README of the repository. Once the extension has been compiled and is available in your system, you will need to load it to your php.ini. You will need to add this line:
Alternatively some distributions add a number prefix on ini files. If that is the case, choose a high number for Phalcon (e.g. 50-phalcon.ini).
Download the latest zephir.phar from here. Add it to a folder that can be accessed by your system.
Clone the repository
git clone https://github.com/phalcon/cphalcon
git checkout tags/v4.0.0-alpha1 ./
The Filter component has been rewritten, utilizing a service locator. Each sanitizer is now enclosed on its own class and lazy loaded to provide maximum performance and the lowest resource usage as possible.
The Phalcon\Filter object has been removed from the framework. In its place we have two components that can help with sanitizing input.
The equivalent of the v3 Phalcon\Filter is now the Phalcon\Filter\FilterLocator object. This object allows you to sanitize input as before using the sanitize() method.
The values sanitized are automatically cast to the relevant types. This is the default behavior for the int, bool and float filters.
When instantiating the locator object, it does not know about any sanitizers. You have two options:
CallingnewInstance() will return a Phalcon\Filter\FilterLocator object with all the sanitizers registered. The sanitizers are lazy loaded so they are instantiated only when called from the locator.
Load only sanitizers you want
You can instantiate the Phalcon\Filter\FilterLocator component and either use the set() method to set all the sanitizers you need, or pass an array in the constructor with the sanitizers you want to register.
Using the FactoryDefault
If you use the Phalcon\Di\FactoryDefault container, then the Phalcon\Filter\FilterLocator is automatically loaded in the container. You can then continue to use the service in your controllers or components as you did before. The name of the service in the Di is filter, just as before.
Also components that utilize the filter service, such as the Request object, transparently use the new filter locator. No additional changes required for those components.
Using a custom Di
If you have set up all the services in the Phalcon\Di yourself and need the filter service, you will need to change its registration as follows:
The Logger component has been rewritten to comply with PSR-3. de tal manera que se puede utilizar con cualquier aplicación que necesite un componente de registro compatible con PSR-3 –incluso sin estar basada en Phalcon.
En Phalcon v3.x el componente trae incorporado el adaptador. Esto en esencia significa que cuando se inicia el objeto de registro, el desarrollador está en realidad creando un adaptador (de archivo, flujo, etc.) con capacidad de registro.
En Phalcon v4 el componente se reescribió de tal manera que se dedica a la función de registro y acepta uno o más adaptadores que serán los responsables de las tareas de registro. Así se logra la compatibilidad con PSR-3, se separan las responsabilidades del componente y se logra la funcionalidad de registro múltiple: fácilmente se puede agregar más de un adaptador al componente, cada uno realizando su propio registro. Con esta implementación se redujo el código del registro y se supimió el componente Logger\Multiple.
The Phalcon\Logger\Multiple component has been removed. You can achieve the same functionality using the logger component and registering more than one adapter:
<?phpusePhalcon\Logger;usePhalcon\Logger\Adapter\Stream;$adapter1=newStream('/logs/first-log.log');$adapter2=newStream('/remote/second-log.log');$adapter3=newStream('/manager/third-log.log');$logger=newLogger('messages',['local'=>$adapter1,'remote'=>$adapter2,'manager'=>$adapter3,]);// Log to all adapters$logger->error('Algo falló');
The getSource() method has been marked as final. As such you can no longer override this method in your model to set the corresponding table/source of the RDBMS. Instead, you can now use the initialize() method and setSource() to set the source of your model.