Home  >  Article  >  PHP Framework  >  The difference between thinkphp3 and thinkphp5

The difference between thinkphp3 and thinkphp5

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-19 15:43:253865browse

The difference between thinkphp3 and thinkphp5

What is the difference between thinkphp3 and thinkphp5? Let me introduce to you the differences between the two:

1. The usage differences between thinkphp3.2 and thinkphp51. The past single-letter functions have been completely replaced, as follows:

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

2. Template rendering:

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

3. Calling the own model in the model:

$this => Db::table($this->table)

4. Naming when creating a new controller and model:

① Remove the suffix controller from the controller: UserController => User

② Remove the suffix model from the model: UserModel => User

5. URL access:

If the controller name uses camel case, you need to link each letter with an underscore before accessing.

eg: The controller name is AddUser, and access is done using add_user

6. In TP5, it supports configuring secondary parameters (i.e. two-dimensional array). In the configuration file, the secondary Configuration parameter reading:

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

②config('user.type');

7. The template supports three Operations of meta-operators:

{$info.status ? $info.msg : $info.error}还支持这种写法:{$varname.aa ?? 'xxx'}或{$varname.aa ?: 'xxx'}

Related recommendations: "

php video tutorial

"8. TP5 built-in tags:

In the system's built-in tags , volist, switch, if, elseif, else, foreach, compare (including all comparison tags), (not) present, (not) empty, (not) defined, etc.

9. TP5 data verification:

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

Note: Use the helper function to instantiate the validator - $validate = validate('User');

10. TP5 implements built-in paging, use the following:

Query user data with status 1, and display 10 pieces of data per page

$list = model('User')->where('status',1)->paginate(10);
$page = $this->render();
$this->assign('_list',$list);
$this->assign('_page',$page);
return $this->fetch();

The paging output code in the template file is as follows:

<div>{$_page}</div>

Second, thinkphp3.2 and thinkphp5 database operations for comparison

Add data

thinkhp3.2

//添加单条数据$User = M(&#39;User&#39;);
$data[&#39;name&#39;] = &#39;ThinkPHP&#39;;
$data[&#39;email&#39;] = &#39;ThinkPHP@gmail.com&#39;;
$User->create($data);
$User->add($data);//添加多条数据$dataList[] = array(&#39;name&#39;=>&#39;thinkphp&#39;,&#39;email&#39;=>&#39;thinkphp@gamil.com&#39;);
$dataList[] = array(&#39;name&#39;=>&#39;onethink&#39;,&#39;email&#39;=>&#39;onethink@gamil.com&#39;)
$User->addAll($dataList);

thinkphp5

//添加单条数据$data = [&#39;foo&#39; => &#39;bar&#39;, &#39;bar&#39; => &#39;foo&#39;];
Db::table(&#39;think_user&#39;)->insert($data);//添加多条数据$data = [
    [&#39;foo&#39; => &#39;bar&#39;, &#39;bar&#39; => &#39;foo&#39;],
    [&#39;foo&#39; => &#39;bar1&#39;, &#39;bar&#39; => &#39;foo1&#39;],
    [&#39;foo&#39; => &#39;bar2&#39;, &#39;bar&#39; => &#39;foo2&#39;]
];
Db::name(&#39;user&#39;)->insertAll($data);

Modify Data

thinkhp3.2

$User = M("User"); // 实例化User对象// 要修改的数据对象属性赋值
$data[&#39;name&#39;] = &#39;ThinkPHP&#39;;$data[&#39;email&#39;] = &#39;ThinkPHP@gmail.com&#39;;
$User->where(&#39;id=5&#39;)->save($data); // 根据条件更新记录
where(&#39;id=5&#39;)->setField(&#39;name&#39;,&#39;ThinkPHP&#39;);
$data = array(&#39;name&#39;=>&#39;ThinkPHP&#39;,&#39;email&#39;=>&#39;ThinkPHP@gmail.com&#39;);// 更改用户的name值
$User-> where(&#39;id=5&#39;)->setField($data);更改用户的name和email的值
$User->where(&#39;id=5&#39;)->setDec(&#39;score&#39;,5); // 用户的积分减5
$User->where(&#39;id=5&#39;)->setInc(&#39;score&#39;,3); // 用户的积分加3

thinkhp5

Db::table(&#39;think_user&#39;)->update([&#39;name&#39; => &#39;thinkphp&#39;,&#39;id&#39;=>1]);//更新数据表中的数据
Db::table(&#39;think_user&#39;) ->where(&#39;id&#39;,1) ->setField(&#39;name&#39;, &#39;thinkphp&#39;);//更新某个字段的值
Db::table(&#39;think_user&#39;)->where(&#39;id&#39;, 1)->setInc(&#39;score&#39;,5);// score 字段加 5
Db::table(&#39;think_user&#39;)->where(&#39;id&#39;, 1)->setDec(&#39;score&#39;, 5);// score 字段减 5
Db::table(&#39;think_user&#39;)->where(&#39;id&#39;, 1)->setInc(&#39;score&#39;, 1, 10);//支持延时更新

Delete data

thinkhp3.2

$User->delete(&#39;1,2,5&#39;); // 删除主键为1,2和5的用户数据
$User->where(&#39;status=0&#39;)->delete(); // 删除所有状态为0的用户数据

thinkphp5

// 根据主键删除Db::table(&#39;think_user&#39;)->delete(1);
Db::table(&#39;think_user&#39;)->delete([1,2,3]);// 条件删除    
Db::table(&#39;think_user&#39;)->where(&#39;id&#39;,1)->delete();
Db::table(&#39;think_user&#39;)->where(&#39;id&#39;,&#39;<&#39;,10)->delete();

Third, comparison between thinkphp5 and thinkphp3. Developers of the .X version can get familiar with and get started with this new version faster. At the same time, it is also strongly recommended that developers abandon their old thinking patterns, because 5.0 is a brand new subversive and reconstructed version. Old ideas of 3. We apologize for the incorrect guidance. In version 5.0, the method similar to /id/1, which can obtain 'id' through 'get', is officially abolished. Strictly speaking, such a URL does not belong to $_GET. Now it can be obtained through 'param' Obtain.

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'.

New version changes

Naming convention

Directory and file names use 'lowercase underscore', And start with a lowercase letter; class libraries and function files are uniformly suffixed with .php; class file names are defined in namespaces, and the path of the namespace is consistent with the path of the class library file (including upper and lower case); class names and class files The names should be consistent, and uniformly use camel case naming (the first letter is capitalized)

FunctionThe system no longer relies on any functions, but only provides assistants for commonly used operation encapsulation Function; single-letter functions are obsolete, and the system loads helper functions by default.

Routing

5.0 URL access no longer supports ordinary URL mode, and routing does not support regular routing definitions. Instead, all are changed to rule routing with variable rules (regular definition), the specific details will not be repeated 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 (modifiable) instead of module name; the class name of the controller does not have the Controller suffix by default. You can configure the controller_suffix parameter to enable the controller class suffix; the controller operation method Use the return method 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;;
    }
}
5.0 version controller writing method

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

3.2 version controller naming

IndexController.class.php

5.0 version controller naming

Index.php

Correct output template in the controller

5.0在控制器中输出模板,使用方法如下:

如果你继承think\Controller的话,可以使用:

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

如果你的控制器没有继承 think\Controller的话,使用:

return view(&#39;index/hello&#39;);

模型

如果非要对比与旧版本的改进,模型被分为数据库、模型、验证器三部分,分别对应M方法、模型、自动验证,同时均有所加强,下面做简单介绍。

数据库

5.0的数据库查询功能增强,原先需要通过模型才能使用的链式查询可以直接通过Db类调用,原来的M函数调用可以改用db函数,例如:
3.2版本

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

5.0版本

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

模型

新版的模型查询增加了静态方法,例如:

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

自动验证

对比旧的版本,可以理解为之前的自动验证且不同于之前的验证;
ThinkPHP5.0验证使用独立的\think\Validate类或者验证器进行验证,不仅适用于模型,在控制器也可直接调用。

配置文件

新版对配置很多的配置参数或者配置层次都和之前不同了,建议大家要么看看代码,要么仔细通读下官方的开发手册,不要因为配置的问题浪费自己一整天的时间。

异常

5.0对错误零容忍,默认情况下会对任何级别的错误抛出异常,并且重新设计了异常页面,展示了详尽的错误信息,便于调试。

系统常量的废弃

5.0版本相对于之前版本对系统变化进行了大量的废弃,用户如果有相关需求可以自行定义
下面是废除常量

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等

部分常量可以在Request里面进行获取

助手函数

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

The difference between thinkphp3 and thinkphp5

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

Statement:
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