Home > PHP Framework > ThinkPHP > body text

What is the difference between thinkphp3 and 5?

爱喝马黛茶的安东尼
Release: 2019-08-26 15:36:59
Original
3631 people have browsed it

What is the difference between thinkphp3 and 5?

3.X old ideas that need to be abandoned

URL changes

First of all, I would like to apologize for the incorrect guidance that the laxity of 3. The 'get' method is used to obtain 'id'. Strictly speaking, such URL does not belong to $_GET. It can now be obtained through 'param'. The specific use can be queried through the request part.

Model changes

The new version of the model query returns the default 'object', and the system adds the 'toArray' method by default. Many developers use 'all' or 'select' 'Try to use 'toArray' to convert to an array. I hope developers can understand the concept of 'object', try to use 'object' to use data, or use the 'db' method to operate the database, and also remind you of this part' For developers who abuse 'toArray', the result of 'all' or 'select' is an array collection of objects, which cannot be converted using 'toArray'.

Related recommendations: "ThinkPHP Tutorial"

New version changes

##Naming convention

· Directory and file names use 'lowercase underscore' and start with a lowercase letter;

· Class libraries and function files The suffix is ​​.php;

·The file names of classes are all defined in namespaces, and the path of the namespace is consistent with the path of the class library file (including upper and lower case);

·The class name and the class file name should be consistent, and uniformly use camel case naming (the first letter is capitalized)

Function

·The system no longer relies on any functions, but only provides helper functions for commonly used operation packages;

·The single-letter function is obsolete, and the system loads the helper function by default. Please refer to Previous chapter 'Helper Function';

routing

5.0 URL access no longer supports ordinary URL mode, routing does not support regular routing definitions, but all changes The method of matching variable rules (regular definitions) for rule routing will not be detailed here.

Controller

The namespace of the controller has been adjusted, and there is no need to inherit any controller class.

·The namespace of the application class library is unified as app (can be modified) instead of the module name;

·The default class name of the controller Without the Controller suffix, you can configure the controller_suffix parameter to enable the controller class suffix;

·The controller operation method uses return mode to return data instead of direct output;

·Abolish the original pre- and post-operation methods;

Version comparison

3.2 Version controller writing method

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller 
{
    public function hello()
    {
        echo &#39;hello,thinkphp!&#39;;
    }
}
Copy after login

5.0 version control Device writing method

namespace app\index\controller;
class Index 
{
    public function index()
    {
        return &#39;hello,thinkphp!&#39;;
    }
}
Copy after login

3.2 version controller naming

IndexController.class.php
Copy after login

5.0 version controller naming

Index.php
Copy after login

**Correct output template in the controller**

5.0 Output the template in the controller, the usage method is as follows:

If you inherit think\Controller, you can use:

return $this->fetch(&#39;index/hello&#39;);
Copy after login

If your controller does not inherit For think\Controller, use:

return view(&#39;index/hello&#39;);
Copy after login

Model

If you have to compare the improvements with the old version, the model is divided into three parts: database, model, and validator. They correspond to M method, model, and automatic verification respectively, and they have all been enhanced. Here is a brief introduction.

Database

The database query function of 5.0 has been enhanced. The chain query that originally needed to be used through the model can be called directly through the Db class. The original M function call can be changed. Use db function, for example:

3.2 version

M(&#39;User&#39;)->where([&#39;name&#39;=>&#39;thinkphp&#39;])->find();
Copy after login

5.0 version

db(&#39;User&#39;)->where(&#39;name&#39;,&#39;thinkphp&#39;)->find();
Copy after login

model

The new version of model query adds static Methods, for example:

User::get(1); 
User::all();
User::where(&#39;id&#39;,&#39;>&#39;,10)->find();
Copy after login

The model part has enhanced many functions, please refer to the "Model Chapter" for details.

Automatic verification

Compared with the old version, it can be understood as the previous automatic verification and different from the previous verification;

ThinkPHP5.0 verification is used The independent \think\Validate class or validator is used for verification, which is not only applicable to the model, but can also be directly called in the controller. Please refer to the "Verification" chapter for specific usage rules, which will not be repeated here.

Configuration file

The new version has many configuration parameters or configuration levels that are different from before. It is recommended that you either take a look at the code or carefully read through the official development Manual, don’t waste your whole day on configuration issues.

Exception

5.0 has zero tolerance for errors. By default, exceptions will be thrown for any level of error, and the exception page has been redesigned to display detailed errors. information to facilitate debugging.

Abandonment of system constants

Compared with previous versions, version 5.0 has made a large number of discards of system changes. Users can define them themselves if they have relevant needs

下面是废除常量

REQUEST_METHOD IS_GET IS_POST IS_PUT IS_DELETE IS_AJAX __EXT__ COMMON_MODULE MODULE_NAME CONTROLLER_NAME ACTION_NAME 
APP_NAMESPACE APP_DEBUG MODULE_PATH等
Copy after login

部分常量可以在Request里面进行获取,具体参考“请求章节”。

注:再次说明本章节仅仅为之前使用3.X版本开发者快速理解5.0所写,具体5.0的功能还需要开发者通读手册。

助手函数

5.0 助手函数和 3.2 版本的单字母函数对比如下:

What is the difference between thinkphp3 and 5?

1、过去的单字母函数已完全被替换掉,如下:

S=>cache,C=>config,M/D=>model,U=>url,I=>input,E=>exception,L=>lang,A=>controller,R=>action
Copy after login

2、模版渲染:

$this->display() => return view()/return $this->fetch();
Copy after login

3、在model中调用自身model:

$this => Db::table($this->table)
Copy after login

4、在新建控制器与模型时的命名:

①控制器去掉后缀controller:UserController => User

②模型去掉后缀model:UserModel => User

5、url访问:

如果控制器名使用驼峰法,访问时需要将各字母之间用下划线链接后进行访问。

eg:控制器名为AddUser,访问是用add_user来进行访问

6、在TP5中支持配置二级参数(即二维数组),配置文件中,二级配置参数读取:

①Config::get('user.type');

②config('user.type');

7、模板中支持三元运算符的运算:{$info.status ? $info.msg : $info.error}还支持这种写法:{$varname.aa ?? 'xxx'}或{$varname.aa ?: 'xxx'}

8、TP5内置标签:

系统内置的标签中,volist、switch、if、elseif、else、foreach、compare(包括所有的比较标签)、(not)present、(not)empty、(not)defined等。

9、TP5数据验证:

$validate = new Validate([&#39;name&#39; => &#39;require|max:25&#39;,&#39;email&#39; => &#39;email&#39;]);
$data = [&#39;name&#39; => &#39;thinkphp&#39;,&#39;email&#39; => &#39;thinkphp@qq.com&#39;];
if(!validate->check($data)){
debug::dump($validate->getError());
}
Copy after login

注:使用助手函数实例化验证器——$validate = validate('User');

10、TP5实现了内置分页,使用如下:

查询状态为1的用户数据,且每页显示10条数据

$list = model(&#39;User&#39;)->where(&#39;status&#39;,1)->paginate(10);
$page = $this->render();
$this->assign(&#39;_list&#39;,$list);
$this->assign(&#39;_page&#39;,$page);
return $this->fetch();
Copy after login

模板文件中分页输出代码如下:

<div>{$_page}</div>
Copy after login

The above is the detailed content of What is the difference between thinkphp3 and 5?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template