Thinkphp5.0

路由route.php

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
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Route;

//模型操作
Route::get('Add','index/User/Add');
Route::get('Del/:id','index/User/Del');
Route::get('Update','index/User/Update');
Route::get('Info/:id','index/User/Info');
Route::get('List','index/User/List');
//模型验证validate
Route::get('Validate','index/User/Validatetext');

//参数validate
return [
'__pattern__' => [
'name' => '\w+',
'id' => '\d+',
],
];

控制器

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\User as UserModel;
use app\index\validate\User as validateuser;

class User extends Controller{
/*@validate验证 */
public function Validatetext(){

$data = array(
'name' =>"Seefly",
'email' =>"qqqqqqqq@qq.com",
'phone' =>"17353953434",
'number' =>"4545",
'nums' =>22,
);
$validate = new validateuser;
if(!$validate->check($data)){
dump($validate->getError());
}else{
echo "验证通过";
}

return ;
}
/*
@添加
@聚合模型
*/
public function Add(){
$usermodel = new UserModel;
$usermodel->name ="我的世界";
$usermodel->password ="123123";
$usermodel->email ="qq@qq.com";
$usermodel->message ="我的世界";
//关联表字段profile
$usermodel->profile="text";
$usermodel->save();
echo $usermodel->id;

}
/*@删除ID*/
public function Del(){

$id = input('id');
if(empty($id)){
return "参数异常!";
}
$usermodel = UserModel::get($id);
if(!empty($usermodel)){
dump($usermodel->delete());
}else{
return "数据以删除";
}
}
/*@修改 */
public function Update(){
$id = 2;
$usermodel = UserModel::get($id);
$usermodel->name ="Seefly";
$usermodel->password ="123123";
$usermodel->email ="Seefly@qq.com";
$usermodel->message ="Seefly";
//关联表字段profile
$usermodel->profile ="Seefly";
$usermodel->save();
return $usermodel->id;
}
/*@一条数据 */
public function Info(){
$id = input('id');
$usermodel = UserModel::get($id);
//$usermodel = UserModel::get(["name"=>"Seefly"]);
return $usermodel;
}
/*@获取数据 */
public function List(){
dump(Request()->isget());
$page = 1;
$pagesize = 3;
$offset = ($page-1)*$page;
$usermodel = new UserModel;
$where = "";

$list = $usermodel->where($where)->limit($offset,$pagesize)->select();

foreach($list as $key=>$val){
dump($key.$val);
}

}

}

控制器原生和内构造器

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
/*
@原生语句
@构造器构造器语句
@构造器

class Userquery extends Controller{
public function Add(){
/*原生sql */
$sql = "insert into user (`name`,`email`,`message`) values('nginx','nginx@qq.com','nginx')";
$res = Db::execute($sql);
/*think 构造器 */
$user = Db::name('user');
$data = array("name"=>"php","email"=>"php@qq.com","message"=>"php");
$res = $user->insert($data);

return $res;
}
public function Update(){
/*原生sql */
$sql = "update user set `name`='GG' where `id` = 29";
$res = Db::execute($sql);
/*think 构造器 */
$user = Db::name('user');
$data = array("name"=>"php111","email"=>"php1@qq.com","message"=>"php1");
$where = array("id"=>"29");
$user->where($where)->update($data);
return $res;
}
public function Del(){
/*原生sql */
$sql = "delete from user where `id` = 28";
//$res = Db::execute($sql);
/*think 构造器 */
$user = Db::name('user');
$where = array("id"=>"18");
$res = $user->where($where)->delete();

return $res;
}
public function Info(){
$sql = "select * from user where `id` = 27";
//$res = Db::query($sql);
/*think 构造器 */
$user = Db::name('user');
$where = array("id"=>array("eq","27"));
$res = $user->where($where)->find();
dump($res);
}
public function List(){
dump(Request()->isget());
$sql = "select * from user order by id desc limit 0,10";
//$res = Db::query($sql);
/*think 构造器 */
$user = Db::name('user');
$where = array('id'=>array('egt','30'));
$res = $user->where($where)->order("id desc")->limit(0,5)->select();
dump($res);
}
/*@表连接 */
public function JoinList(){
dump(Request()->isget());
$sql = "select a.id,p.id,a.name,a.email,p.profile from user as a join profile as p on a.id = p.uid order by a.id desc limit 0,10";
//$res = Db::query($sql);
/*think 构造器 */
$user = Db::name('user as a');
$where = array('a.id'=>array('egt','30'));
$res = $user->join('profile as b','a.id=p.uid','left')->where($where)->order('a.id desc')->limit(0,5)->select();
dump($res);
}
}

Model定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
namespace app\index\model;
use think\model\Merge;

class User extends Merge{
// 设置主表名
protected $table = 'user';
// 定义关联模型列表
protected $relationModel = [
// 给关联模型设置数据表
'Profile' => 'profile',
];
// 定义关联外键
protected $fk = 'uid';
protected $mapFields = [
// 为混淆字段定义映射
'id' => 'User.id',
'profile_id' => 'Profile.id',
];
}

validate定义使用

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace app\index\validate;
use think\Validate;
use think\Db;
/*@validate校验规则*/
class User extends Validate{

protected $rule = [
['name','require|max:25|boolName','姓名不能为空|姓名最大长度不能超过25|名称已存在'],
['email','email','邮箱格式错误'],
['phone','isMobile','手机格式错误'],
['number','require|number|max:25','number不能为空|数字类型|最大长度25'],
['nums','isNums','不是22'],
];
/*
@自定义校验规则
*/

/*校验这个nums只能是22的 */
protected function isNums($val){
if($val==22){
return true;
}
return false;
}

/*校验手机号 */
protected function isMobile($val)
{
$rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
$result = preg_match($rule, $val);
if ($result) {
return true;
} else {
return false;
}
}
/*@校验数据库中是否存在 */
protected function boolName($val){
$total = Db::name('user')->where("name",$val)->count();
if ($total>0){
return false;
}
return true;
}

}