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. This allows you to use the Phalcon\Logger to any application that utilizes a PSR-3 logger, not just Phalcon based ones.
In v3, the logger was incorporating the adapter in the same component. So in essence when creating a logger object, the developer was creating an adapter (file, stream etc.) with logger functionality.
For v4, we rewrote the component to implement only the logging functionality and to accept one or more adapters that would be responsible for doing the work of logging. This immediately offers compatibility with PSR-3 and separates the responsibilities of the component. It also offers an easy way to attach more than one adapter to the logging component so that logging to multiple adapters can be achieved. By using this implementation we have reduced the code necessary for this component and removed the old Logger\Multiple component.
Creating a logger component
<?phpusePhalcon\Logger;usePhalcon\Logger\Adapter\Stream;$adapter=newStream('/logs/application.log');$logger=newLogger('messages',['main'=>$adapter,]);$logger->error('Something went wrong');
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('Something went wrong');
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.