Home>Article>PHP Framework> 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 tutorialIn 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:
{$_page}
Second, thinkphp3.2 and thinkphp5 database operations for comparison
Add datathinkhp3.2
//添加单条数据$User = M('User'); $data['name'] = 'ThinkPHP'; $data['email'] = 'ThinkPHP@gmail.com'; $User->create($data); $User->add($data);//添加多条数据$dataList[] = array('name'=>'thinkphp','email'=>'thinkphp@gamil.com'); $dataList[] = array('name'=>'onethink','email'=>'onethink@gamil.com') $User->addAll($dataList);
//添加单条数据$data = ['foo' => 'bar', 'bar' => 'foo']; Db::table('think_user')->insert($data);//添加多条数据$data = [ ['foo' => 'bar', 'bar' => 'foo'], ['foo' => 'bar1', 'bar' => 'foo1'], ['foo' => 'bar2', 'bar' => 'foo2'] ]; Db::name('user')->insertAll($data);Modify Data
thinkhp3.2
$User = M("User"); // 实例化User对象// 要修改的数据对象属性赋值 $data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com'; $User->where('id=5')->save($data); // 根据条件更新记录 where('id=5')->setField('name','ThinkPHP'); $data = array('name'=>'ThinkPHP','email'=>'ThinkPHP@gmail.com');// 更改用户的name值 $User-> where('id=5')->setField($data);更改用户的name和email的值 $User->where('id=5')->setDec('score',5); // 用户的积分减5 $User->where('id=5')->setInc('score',3); // 用户的积分加3
Db::table('think_user')->update(['name' => 'thinkphp','id'=>1]);//更新数据表中的数据 Db::table('think_user') ->where('id',1) ->setField('name', 'thinkphp');//更新某个字段的值 Db::table('think_user')->where('id', 1)->setInc('score',5);// score 字段加 5 Db::table('think_user')->where('id', 1)->setDec('score', 5);// score 字段减 5 Db::table('think_user')->where('id', 1)->setInc('score', 1, 10);//支持延时更新Delete data
thinkhp3.2
$User->delete('1,2,5'); // 删除主键为1,2和5的用户数据 $User->where('status=0')->delete(); // 删除所有状态为0的用户数据
// 根据主键删除Db::table('think_user')->delete(1); Db::table('think_user')->delete([1,2,3]);// 条件删除 Db::table('think_user')->where('id',1)->delete(); Db::table('think_user')->where('id','<',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 changesThe 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 comparison3.2 version controller writing method
5.0 version controller writing method
namespace app\index\controller; class Index { public function index() { return 'hello,thinkphp!'; } }
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('index/hello');
如果你的控制器没有继承 think\Controller的话,使用:
return view('index/hello');
模型
如果非要对比与旧版本的改进,模型被分为数据库、模型、验证器三部分,分别对应M方法、模型、自动验证,同时均有所加强,下面做简单介绍。
数据库
5.0的数据库查询功能增强,原先需要通过模型才能使用的链式查询可以直接通过Db类调用,原来的M函数调用可以改用db函数,例如:
3.2版本
M('User')->where(['name'=>'thinkphp'])->find();
5.0版本
db('User')->where('name','thinkphp')->find();
模型
新版的模型查询增加了静态方法,例如:
User::get(1); User::all(); User::where('id','>',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 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!