2022-10-31 01:47:12 +08:00
# 2.1 Upgrade Guide
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
- Version 2.1 mainly adds the underlying driver [hyperf/engine ](https://github.com/hyperf/engine ), allowing the `Hyperf` framework to run on top of `Swoole` or `Swow` .
- Minimum PHP version changed from 7.2 to 7.3
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
> Swow is temporarily a preview version, please use it with caution.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
## Modify Hyperf component version
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
Simply change `hyperf/*` in `composer.json` to `2.1.*` .
2022-02-16 02:13:55 +08:00
```json
{
"require": {
"php": ">=7.3",
"ext-json": "*",
"ext-openssl": "*",
"ext-pdo": "*",
"ext-pdo_mysql": "*",
"ext-redis": "*",
"ext-swoole": ">=4.5",
"hyperf/async-queue": "2.1.*",
"hyperf/cache": "2.1.*",
"hyperf/command": "2.1.*",
"hyperf/config": "2.1.*",
"hyperf/constants": "2.1.*",
"hyperf/contract": "2.1.*",
"hyperf/database": "2.1.*",
"hyperf/db-connection": "2.1.*",
"hyperf/di": "2.1.*",
"hyperf/dispatcher": "2.1.*",
"hyperf/event": "2.1.*",
"hyperf/exception-handler": "2.1.*",
"hyperf/framework": "2.1.*",
"hyperf/guzzle": "2.1.*",
"hyperf/http-server": "2.1.*",
"hyperf/logger": "2.1.*",
"hyperf/model-cache": "2.1.*",
"hyperf/pool": "2.1.*",
"hyperf/process": "2.1.*",
"hyperf/redis": "2.1.*",
"hyperf/server": "2.1.*",
"hyperf/utils": "2.1.*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"hyperf/devtool": "2.1.*",
"hyperf/testing": "2.1.*",
"mockery/mockery": "^1.0",
"phpstan/phpstan": "^0.12.18",
"swoole/ide-helper": "dev-master",
"symfony/var-dumper": "^5.1"
}
}
```
2022-10-31 01:47:12 +08:00
After that, you only need to execute `composer update -o` , and the upgrade can be completed normally.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
## Add optimize-autoloader configuration
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
Because `Hyperf` needs to use `class_map` generated by `composer` since `2.0` , which requires users to use `-o` for optimization every time they update dependencies, but many users never have this habit.
Therefore, we recommend adding corresponding configuration to `composer.json` to meet this need.
2022-02-16 02:13:55 +08:00
```json
{
"config": {
"optimize-autoloader": true,
"sort-packages": true
}
}
```
2022-10-31 01:47:12 +08:00
## Modify SwooleEvent
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
The class `Hyperf\Server\SwooleEvent` has been renamed to `Hyperf\Server\Event` in `2.1` , so we need to modify the corresponding code in the `server.php` configuration.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
> SwooleEvent will be officially removed in 3.0, please change it to Event as soon as possible
2022-02-16 02:13:55 +08:00
```php
< ?php
declare(strict_types=1);
use Hyperf\Server\Event;
use Hyperf\Server\Server;
return [
'mode' => SWOOLE_BASE,
'servers' => [
[
'name' => 'http',
'type' => Server::SERVER_HTTP,
'host' => '0.0.0.0',
'port' => 9501,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
],
],
],
'callbacks' => [
Event::ON_BEFORE_START => [Hyperf\Framework\Bootstrap\ServerStartCallback::class, 'beforeStart'],
Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
],
];
```
2022-10-31 23:43:05 +08:00
## Paginator
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
Because the component `hyperf/paginator` has been removed from the `hyperf/database` dependency. Therefore, students who use the pager in the database also need to introduce the `hyperf/paginator` component.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
## Modify DBAL version
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
If you use the `doctrine/dbal` component, you need to upgrade to the `^3.0` version.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
## remove component doctrine/common
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
The `doctrine/common` component has a dependency conflict with `hyperf/utils` . So this component needs to be removed from `composer.json` .
2022-02-16 02:13:55 +08:00
```bash
2022-10-31 01:47:12 +08:00
# remove component
2022-02-16 02:13:55 +08:00
composer remove doctrine/common
2022-10-31 23:43:05 +08:00
# update
2022-02-16 02:13:55 +08:00
composer update "hyperf/*" -o
```
2022-10-31 01:47:12 +08:00
## Precautions
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
- Try not to change the engine of the old project to Swow, if you want to use Swow, please try it in the new project. Because Swow is not a substitute for Swoole, not all Swoole scenarios can find corresponding alternatives in Swow.