[ThinkPHP6 series learning] Changes between TP6 and TP5

Table of contents

1. Application mode

2. Template rendering

3. Template jump redirection


1. Application mode

TP6: Single application mode by default. If you enable multi-application mode, you need to download relevant dependency packages.

TP5: Multi-application mode

① Turn on multi-application mode

composer requiretopthink/think-multi-app

② Create application

php think build 应用名称(例:index或者admin)

2. Template rendering

Because TP5 canceled the \think\Controller class, using the $this->assign() and $this->fetch() methods to assign variables and render templates can no longer be used, so you need to introduce relevant classes in the controller and use the class name Use the form ::method name .

TP5: Directly use helper functions to assign variables and render templates

① Integrated class: If your controller inherits \think\Controllerthe class, you do not need to instantiate the view class yourself. You can directly call the methods of the related view classes encapsulated by the controller base class.

use \think\Controller

② Assign variables

// 模板变量赋值
$this->assign('name','ThinkPHP');
$this->assign('email','[email protected]');

③ Template rendering

// 渲染模板输出
return $this->fetch('hello');

TP6: Need to download dependency packages

①Download dependent libraries

composer require topthink/think-view

② Controller reference class file

use think\facade\View;

③ Assign variables:

//	模板变量赋值
View::assign('name','ThinkPHP');
View::assign('email','[email protected]');

//	或者批量赋值
View::assign([
	'name'		=>	'ThinkPHP',
	'email'	=>	'[email protected]'
});

④ Template rendering

//	模板输出
return	View::fetch('index');

// 或者使用助手函数
return	view('index');

3. Template jump redirection

Because TP6 canceled  the \think\Controller class, the system no longer provides the basic control class \think\Controller. The original success, error, redirect, and result methods need to be implemented in the basic controller class.

Guess you like

Origin blog.csdn.net/qq_25285531/article/details/130759046