composer
因为我用的是phpstudy,首先要切换到php7以上要安装vc14,结果安装失败,原因是因为我安装了VS2017,然后我就有了VC2017的版本,我在控制面板把64版本的VC2017卸载,然后重新安装vc14成功。phpstudy2016自带composer1.2版本,开启openssl扩展,执行composer self-update报错:1
2
3
4
5
6Updating to version 1.8.6 (stable channel).
Downloading: 100%
[RuntimeException]
SHA384 is not supported by your openssl extension, could not verify the phar file integrity
我就只好自己安装了地址,安装里填的是7.2版本的php.exe,因为6.0版本的tp只支持7.1以上的php。
安装框架
切换到WWW目录,安装tp框架1
composer create-project topthink/think tp6 6.0.*-dev
测试执行,进入到tp6文件夹下,执行1
php think run
目录结构
- 框架源码的位置:vendor
- 应用目录名称与命名空间一致了:app
- 其他目录结构与文件功能保持5.1版本一致
配置设置
通过create-project默认安装的话, 会在根目录自带一个.example.env文件,直接更名为.env文件就可以了。填好数据库配置
,打开config下的database.php,添加:1
use think\facade\Env;
然后conroller下index做个测试1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace app\index\controller;
use app\BaseController;
use think\facade\Env;
use think\facade\Db;
class Index extends BaseController
{
public function index()
{
$query = 'show database';
dump(Env::get('database.hostname'));
dump(Db::query("show databases"));
}
public function hello($name = 'ThinkPHP6')
{
return 'hello,' . $name;
}
}
可以看到成功输出
数据库
查询构造器1
2
3
4
5
6
7
8
9
10
11
12
13
14//table():设置数据表
//field():设置查询字段列表
//find():返回满足条件的第一条记录
//select():返回满足条件的多条记录
//where():设置查询条件,字符串,表达式,数组
//fetchSql():true,会拦截执行,返回构造好的sql语句
$res=Db::table('user')
->field('username,age')
->where('username=chenxiyuan')
->select();
dump($res)
//区间,模糊查询:->where('age','between',[10,20])
//关联数组,等值查询,AND:->where(['user_id'=>2,'age'=>18])
//order('age',' desc') asc升序,desc降序,多个字段升降序要放在一个数组中
模型
创建一个模型类,User.php1
2
3
4
5
6
7
8
9
10
11
12
13
namespace app\index\model;
use think\Model;
class User extends Model
{
protected $table ='user';
protected $pk ='user_id';
}
创建一个控制器,ModelTest.php1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
namaspace app\index\controller;
use app\index\model\User;
class ModelTest
{
//模型对象
public function demo1()
{
//创建模型对象
$user = new user();
$res=$user->db()->find(1);
dump($res);
}
//依赖注入
public function demo2(User $user)
{
$res=$user->db()->find(3);
echo $res['username'];
echo $res->name;
}
//create():参数就是要新增的数据,返回当前模型对象
public function insert()
{
$data=['username'=>'chenxiyuan','password'='123456']
$user = User::create($data);
return $user['user_id'];
}
//查询,直接用db()来调用Query类中的方法完成
public function select(User $user)
{
$res=$user->db()->find(1);
dump($res);
}
//更新:update
public function update()
{
$user = User::update(['username'=>'chenxiyuan'],['password'=>'111111']);
}
//删除:destroy(),返回布尔值
public function delete()
{
$res = User::destroy(function ($query){
$query->where('username'='chenxiyuan');
});
$res = User::destroy(['user_id'=>1]);
}
}
视图与模板
示例
新建一个ViewTest控制器,目录为app/index/controller/ViewTest.php1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
namespace app\index\controller;
use think\View;
class ViewTest
{
//方法与模板文件对应
public function index(View $view)
{
//默认的视图目录是view
//return $view->fetch('file:///D:/phpStudy/WWW/tp6/view/index/viewtest/index.html');
return $view->fetch();
}
}
模板路径在与app同级的view下,路径为view/index/viewtest/index.html。如果方法与模板文件名对应,那么fetc()可为空,如果不对应,则要自己填模板名(不用填.html),当然填实际路径也可以。
创建完成后,访问127.0.0.1:8000/index/viewtest/index
模板赋值
添加fuzhi方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public function fuzhi(View $view)
{
$view->assign('site','晨曦远的个人博客');
//一组变量
$view->assign(['username'=>'chenxiyuan','email'=>'969987508@qq.com']);
//数组
$info = ['grade'=>'1','age'=>19,'sex'=>'boy'];
$view->assign('info',$info);
//对象
$obj = new \stdClass();
$obj->course = 'math';
$obj->score = 60;
$view->assign('stu',$obj);
//预定义变量: $_GET, $_SERVER
return $view->fetch();
}fuzhi.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板赋值</title>
</head>
<body>
{// 注释}
{// 花括号将变量包裹即可访问变量}
<p>{$site}</p>
<p>用户名:{$username}</p>
<p>邮箱:{$email}</p>
{// 数组}
<p>grade:{$info['grade']}</p>
<p>age:{$info['age']}</p>
<p>sex:{$info.sex}</p>
{// 对象}
<p>course:{$stu->course}</p>
<p>score:{$stu->score}</p>
{// 预定义变量}
<p>id:{$_GET['id']}</p>
<p>id:{$Think.get.id}</p>
</body>
</html>
流程控制
循环
1
2
3
4
5
6
7{foreach $users as $key=>$user}
<p>{$key}:{$user['name']}--{$user['age']}</p>
{/foreach}
{volist name="users" id="user"}
<p>{$key}:{$user['name']}--{$user['age']}</p>
{/volist}判断
1
2
3{if (条件)}
...
{/if}其他内置标签
标签名 | 作用 | 包含属性 |
---|---|---|
include | 包含外部模板文件(闭合) | file |
load | 导入资源文件(闭合 包括js css import别名) | file,href,type,value,basepath |
volist | 循环数组数据输出 | name,id,offset,length,key,mod |
foreach | 数组或对象遍历输出 | name,item,key |
for | For循环数据输出 | name,from,to,before,step |
switch | 分支判断输出 | name |
case | 分支判断输出(必须和switch配套使用) | value,break |
default | 默认情况输出(闭合 必须和switch配套使用) | 无 |
compare | 比较输出(包括eq neq lt gt egt elt heq nheq等别名) | name,value,type |
range | 范围判断输出(包括in notin between notbetween别名) | name,value,type |
present | 判断是否赋值 | name |
notpresent | 判断是否尚未赋值 | name |
empty | 判断数据是否为空 | name |
notempty | 判断数据是否不为空 | name |
defined | 判断常量是否定义 | name |
notdefined | 判断常量是否未定义 | name |
define | 常量定义(闭合) | name,value |
assign | 变量赋值(闭合) | name,value |
if | 条件判断输出 | condition |
elseif | 条件判断输出(闭合 必须和if标签配套使用) | condition |
else | 条件不成立输出(闭合 可用于其他标签) | 无 |
php | 使用php代码 | 无 |
模板继承
{block} {/block}
组成区块,子模板可以对区块进行重载
base.html
1
2
3
4
5
6
7
8
9
10<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>标题</title>
</head>
<body>
{block name="menu"}菜单{/block}
{block name="footer"}底部{/block}
</body>
</html>子模板:index.html
1
2
3
4
5
6
7
8
9
10{extend name="base" /}
{block name="menu"}
<a href="/" >首页</a>
<a href="/home/" >主页</a>
<a href="/about/" >关于</a>
{/block}
{block name="footer"}
{__block__}合并
{/block}
在子模板中,可以对基础模板中的区块进行重载定义,如果没有重新定义的话,则表示沿用基础模板中的区块定义,如果定义了一个空的区块,则表示删除基础模板中的该区块内容。{__block__}
这个标签,当区块中有这个标记时,就不只是直接重载这个区块,它表示引用所继承模板对应区块的内容到这个位置,最终这个区块是合并后的内容。所以显示的是底部合并