想着将就项目从tp5.1升级到tp6,哪知道遇到这么多坑,因为tp6不是写给MVC结构,记录下
如果使用指令composer create-project topthink/think tp安装,不能完全安装。需要添加--ignore-platform-reqs。
//安装
composer create-project topthink/think rent6 --ignore-platform-reqs
// 更新
composer update --ignore-platform-reqs
PS:有可能是php安装有问题,php8需要添加fileinfo的扩展
tp6默认单应用模式
composer require topthink/think-multi-app
tp6默认不支持模板引擎(view),需要另外安装
composer require topthink/think-view
// 开启模板布局
View::config(['layout_on' => true]);
// 布局文件名称
View::config(['layout_name' => 'common/layout']);
在app\admin\config\view.php
设置
[
'__STATIC__' => '/static/admin',
'__CDN__' => '/static/common',
'__REPORT__' => '/static/reports',
],
];
tp6里$this->success()
、$this->error()
、redirect()
这种转跳方法已取消,需自行安装旧扩展。
第一步:
composer require liliuwei/thinkphp-jump
第二步:在BaseController.php里引入
use \liliuwei\think\Jump;
另外,redirect()的调用如下:
$this->redirect(url('Index/login'));
get
/all
方法无论使用Db
类还是模型类查询,全部统一使用find
/select
方法,取消了之前模型类额外提供的get
/all
方法。同时取消的方法还包括getOrFail
/allOrFail
。
tp6中Session
功能默认是没有开启的,需要在中间件中开启。
打开app目录下的middleware.php文件,在return中加上:
// Session初始化
\think\middleware\SessionInit::class
另外,sessionID获取方法和初始化如下
// tp5.1
$id = session_id(); // 获取sessionID
Session::init(['id' => $id]); // 初始化
// tp6
Session::getId(); //获取sessionID
Session::setId($id); //设置sessionID
Session::init(); //初始化
php think build admin
打开config里的view.php 添加以下代码
// 开启模板布局
'layout_on' => true,
// 布局文件名称
'layout_name' => 'public/layout',
layout的使用
together()的参数 tp6为数组,tp5.1为字符
// tp5.1
$article->together('comments')->delete();
// tp6
$article->together(['comments'])->delete();
field的用法
// tp5.1
Db::table('think_user')->field('user_id,content',true)->select();
// tp6
Db::table('user')->withoutField('user_id,content')->select();
在tp6中$this->data('salt', $salt);
将影响到其他数值,所以要全部赋值。
// tp5.1
public function setPasswordAttr($value)
{
$salt = md5(uniqid(microtime(), true));
$this->data('salt', $salt);
return md5(md5($value) . $salt);
}
// tp6
public function setPasswordAttr($value, $data)
{
$salt = md5(uniqid(microtime(), true));
$data['salt'] = $salt;
$this->data($data);
return md5(md5($value) . $salt);
}