ThinkPHP6.1 升级到 8.0

最近看见社区非常多小伙伴在问 ThinkPHP6.1 升级到 8.0 的问题,这里我就来简单的介绍一下升级的方法。

首先,我先本地安装个 ThinkPHP6.1 的开发版。

1
$ composer create-project topthink/think=6.1.x-dev thinkphp61

现在查看下项目的依赖信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ cd thinkphp61
$ composer show
league/flysystem 1.1.10 Filesystem abstraction: Many filesystems, one API.
league/flysystem-cached-adapter 1.1.0 An adapter decorator to enable meta-data caching.
league/mime-type-detection 1.11.0 Mime-type detection for Flysystem
psr/cache 1.0.1 Common interface for caching libraries
psr/container 1.1.2 Common Container Interface (PHP FIG PSR-11)
psr/http-message 1.1 Common interface for HTTP messages
psr/log 1.1.4 Common interface for logging libraries
psr/simple-cache 1.0.1 Common interfaces for simple caching
symfony/polyfill-mbstring v1.27.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72 v1.27.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php80 v1.27.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/var-dumper v4.4.47 Provides mechanisms for walking through any arbitrary PHP variable
topthink/framework v6.1.4 The ThinkPHP Framework.
topthink/think-filesystem v1.0.3 The ThinkPHP6.1 Filesystem Package
topthink/think-helper v3.1.6 The ThinkPHP6 Helper Package
topthink/think-orm v2.0.61 think orm
topthink/think-trace v1.6 thinkphp debug trace

贴一下项目的 composer.json 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{
"name": "topthink/think",
"description": "the new thinkphp framework",
"type": "project",
"keywords": [
"framework",
"thinkphp",
"ORM"
],
"homepage": "https://www.thinkphp.cn/",
"license": "Apache-2.0",
"authors": [
{
"name": "liu21st",
"email": "liu21st@gmail.com"
},
{
"name": "yunwuxin",
"email": "448901948@qq.com"
}
],
"require": {
"php": ">=7.2.5",
"topthink/framework": "^6.1.0",
"topthink/think-orm": "^2.0",
"topthink/think-filesystem": "^1.0"
},
"require-dev": {
"symfony/var-dumper": "^4.2",
"topthink/think-trace": "^1.0"
},
"autoload": {
"psr-4": {
"app\\": "app"
},
"psr-0": {
"": "extend/"
}
},
"config": {
"preferred-install": "dist"
},
"scripts": {
"post-autoload-dump": [
"@php think service:discover",
"@php think vendor:publish"
]
}
}

其中 requirerequire-dev 是依赖的信息,里面进行了一些版本的锁定,比如 ^6.1.0 那可以理解为 >=6.1.0 <7.0.0,所以很多小伙伴进行升级的时候因为版本号锁定问题就没办法直接升级,这里我们要对一些依赖进行调整。

修改前 修改后
"php": ">=7.2.5" "php": ">=8.0.0"
"topthink/framework": "^6.1.0" "topthink/framework": "^8.0.0"
"topthink/think-orm": "^2.0" "topthink/think-orm": "^3.0"
"topthink/think-filesystem": "^1.0" "topthink/think-filesystem": "^2.0"
"symfony/var-dumper": "^4.2" "symfony/var-dumper": ">=4.2"

这里我没办法把全部都提供,比如如果你使用 ThinkPHP 官方提供的依赖,那现在大多数都支持 8.0 了,你可以去相关的 github 仓库查看。

在 composer.json 里面修改完依赖后,你还需要把本地的 composer.lock 删除然后再执行 composer install,这样才能保证依赖的版本是最新的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$ rm -f composer.lock
$ composer install
Loading composer repositories with package information
Updating dependencies
Lock file operations: 13 installs, 0 updates, 0 removals
- Locking league/flysystem (2.5.0)
- Locking league/mime-type-detection (1.11.0)
- Locking psr/container (2.0.2)
- Locking psr/http-message (1.1)
- Locking psr/log (3.0.0)
- Locking psr/simple-cache (3.0.0)
- Locking symfony/polyfill-mbstring (v1.27.0)
- Locking symfony/var-dumper (v6.3.1)
- Locking topthink/framework (v8.0.1)
- Locking topthink/think-filesystem (v2.0.2)
- Locking topthink/think-helper (v3.1.6)
- Locking topthink/think-orm (v3.0.10)
- Locking topthink/think-trace (v1.6)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 8 updates, 4 removals
- Downloading topthink/framework (v8.0.1)
- Removing symfony/polyfill-php80 (v1.27.0)
- Removing symfony/polyfill-php72 (v1.27.0)
- Removing psr/cache (1.0.1)
- Removing league/flysystem-cached-adapter (1.1.0)
- Upgrading psr/container (1.1.2 => 2.0.2): Extracting archive
- Upgrading symfony/var-dumper (v4.4.47 => v6.3.1): Extracting archive
- Upgrading psr/simple-cache (1.0.1 => 3.0.0): Extracting archive
- Upgrading psr/log (1.1.4 => 3.0.0): Extracting archive
- Upgrading topthink/think-orm (v2.0.61 => v3.0.10): Extracting archive
- Upgrading topthink/framework (v6.1.4 => v8.0.1): Extracting archive
- Upgrading league/flysystem (1.1.10 => 2.5.0): Extracting archive
- Upgrading topthink/think-filesystem (v1.0.3 => v2.0.2): Extracting archive
Generating autoload files
> @php think service:discover
Succeed!
> @php think vendor:publish
File /code/thinkphp61/config/trace.php exist!
Succeed!
4 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

重新打印下依赖信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ composer show
league/flysystem 2.5.0 File storage abstraction for PHP
league/mime-type-detection 1.11.0 Mime-type detection for Flysystem
psr/container 2.0.2 Common Container Interface (PHP FIG PSR-11)
psr/http-message 1.1 Common interface for HTTP messages
psr/log 3.0.0 Common interface for logging libraries
psr/simple-cache 3.0.0 Common interfaces for simple caching
symfony/polyfill-mbstring v1.27.0 Symfony polyfill for the Mbstring extension
symfony/var-dumper v6.3.1 Provides mechanisms for walking through any arbitrary PHP variable
topthink/framework v8.0.1 The ThinkPHP Framework.
topthink/think-filesystem v2.0.2 The ThinkPHP6.1 Filesystem Package
topthink/think-helper v3.1.6 The ThinkPHP6 Helper Package
topthink/think-orm v3.0.10 the PHP Database&ORM Framework
topthink/think-trace v1.6 thinkphp debug trace

好了,现在就算是升级到 ThinkPHP 8.0 了。

往上