Home > Article > PHP Framework > How to upgrade thinkphp
#Thinkphp version upgrade related introduction, taking the upgrade from 5.0 to 5.1 as an example:
First look at the general directory structure of 5.0:
project 应用部署目录 ├—application 应用目录(可设置) ├—extend 扩展类库目录(可定义) ├—public WEB 部署目录(对外访问目录) ├—runtime 应用的运行时目录(可写,可设置) ├—vendor 第三方类库目录(Composer) ├—thinkphp 框架系统目录
Let’s take a look at the general directory structure of 5.1:
www WEB部署目录(或者子目录) ├—application 应用目录 ├—config 应用配置目录 ├—route 路由定义目录 ├—public WEB目录(对外访问目录) ├—thinkphp 框架系统目录 ├—extend 扩展类库目录 ├—runtime 应用的运行时目录(可写,可定制) project 应用部署目录 ├—application 应用目录(可设置) ├—extend 扩展类库目录(可定义) ├—public WEB 部署目录(对外访问目录) ├—runtime 应用的运行时目录(可写,可设置) ├—vendor 第三方类库目录(Composer) ├—thinkphp 框架系统目录 ├—vendor 第三方类库目录(Composer依赖库)
Through the first layer of directories, we can see that 5.1 only has more config and route directories, indicating that version 5.1 has already included configuration files and router files. Extracted.
Below we use the wstmart open source mall system as an example to try to upgrade.
First we export the latest thinkphp5.1 framework through git:
Let’s run it and see the result:
Import the open source mall wstmart code.
Related recommendations: "ThinkPHP Tutorial"
We cut the wstmart in the wstmart directory of the wstmart open source mall to the tp5 directory, modify the index.php file, and let Run the project directly in the system root directory, then modify the database configuration file and turn on the debugging function, and run it:
Huh? ! ! Something went wrong? Quickly look through the documentation. Scroll down the document and have a look, eh? All right. It turns out that I was too impatient to finish reading it. If I redefine the entry file, I can’t simply use the index.php file that originally pointed to the public directory. I also need to define the application directory, for example: Container::get( 'app')->path(APP_PATH)->run()->send();where APP_PATH is the directory of the application to be defined. I will change it to:
Container::get('app')->path(__DIR__ . '/wstmart/')->run()->send();
Run and see :
Finally normal. The reason for this result is that I did not modify the configuration file and told the system that the default module is home. I then modified the 'default_module' => 'home' in the app.php file and ran it to see:
Um? What's going on with this? The file obviously exists! ! ! I checked the document carefully and found that it does exist...it must be 5.1 and there are some essential features that I missed again, so I went back to read the document. After searching back and forth, I finally found this sentence in the upgrade guide: "If you customize the namespace of the application class library, you need to set the environment variable APP_NAMESPACE instead of the application configuration file. If you use .env configuration file, you can add in it: APP_NAMESPACE = your application library root namespace name." I see! !
So create an .env file under the system. Content text: app_namespace=wstmart
Run it again:
This paragraph means that this function was not found. In the thinkphp5.0 framework, the system will automatically load files in the directory common. Why is it not loaded automatically in 5.1? So I took a look at the source code of thinkphp5.1. See this line:
#Undefined array? Such a weird question?
From the error message below, we can locate the function getFloors called by the index.php file of the module home. The error message is the code:
$rs = Db::name('goods_cats')->where(['dataFlag'=>1, 'isShow' => 1,'parentId'=>['in',$ids],'isFloor'=>1]) ->field("parentId,catName,catId")->order('catSort asc')->select();
There is a problem. Let's look through the documentation again. The document says:
has changed the array multi-field batch query of the query constructor. The original
where(['name'=>['like','think%'], 'id'=>['>',0],])
needs to be adjusted to
where([['name','like','think%'], ['id','>',0],])
. We refer to the above instructions to modify the code. For:
$rs = Db::name('goods_cats')->where([['dataFlag','=',1], ['isShow','=',1],['parentId','in',$ids],['isFloor','=',1]]) ->field("parentId,catName,catId")->order('catSort asc')->select();
Sure enough, this error no longer appears. Next is another question:
# Refer to the document. This function has been modified in thinkphp5.1. We changed thinkRequest to thinkFacadeRequest and the error disappeared again.
The following is an error:
The system can run to this point, indicating that the program has been able to receive and successfully return information. The error on the page should be an error in the custom tag wst:ads, which resulted in no content being returned. We followed the troubleshooting method above and continued to go back to debug. It turned out that we forgot to configure the label during the upgrade. We open the template, add the code 'taglib_pre_load' => 'wstmartcommonTaglibWst', and then refresh the page:
It's a familiar error again, but this time it's a different function , for errors that have already found solutions, we can easily solve them, so we won’t repeat them again. Basically, just modify the query function in the model. After making the changes according to the previous method, we can complete the upgrade from Thinkphp 5.0 to 5.1.
Because of the limited space, I will not take screenshots one by one to debug. Before upgrading, everyone should read the official upgrade guide of thinkphp to avoid filling in one pit after another like me before finally completing the upgrade.
The above is the detailed content of How to upgrade thinkphp. For more information, please follow other related articles on the PHP Chinese website!