雅荷心语博客
雅荷心语
心之所向便是光
  • 首页
  • 前端之旅
  • 后端之路
  • 软件工具
  • 心灵鸡汤
  • 心路历程
  • 视频资料
  • 关于我们
    • 关于我
    • 关于我
  • 微信平台
    • 业务合作
  • 首页
  • 前端之旅
  • 后端之路
  • 软件工具
  • 心灵鸡汤
  • 心路历程
  • 视频资料
  • 关于我们
    • 关于我
    • 关于我
  • 微信平台
    • 业务合作
  • 关注本站
    • 微信
    • 微博
    • 腾讯微博
    • Twitter
    • Facebook
    • RSS订阅
Hi, 请登录     我要注册     找回密码

玩一玩为Api而生的ThinkPHP5.0

2016-10-24 分类:前端之旅 阅读(1653) 评论(0)

今天看了看为Api而生的ThinkPHP5.0, 感觉还不错,想玩好这个框架,就先抛弃之前对thinkphp的认识吧,这个变化很大,

路由什么的有点麻烦,就先用默认的;路由,看了看ORM,模型和控制器等等~~

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace app\index\controller;
use app\common\model\produce;
use app\common\model\test;
use think\Controller;
use think\Request;
use think\Db;
use think\Cache;
use think\Session;
use think\Cookie;
use think\File;
 
class Index extends Controller
{
    public function _empty($name){
        return $this->showCity($name);
    }
    protected function showCity($name)
    {
        //和$name这个城市相关的处理
        return '当前城市' . $name;
    }
    public function edit($id)
    {
        return 'edit:'.$id;
    }
    public function Request()
    {
        Request::instance()->has('id','get');
        Request::instance()->has('name','post');
    }
    public function index(){
        $data = db('produce')->where('produce_id','>',1)->find();
        $data = Db::table('produce')->where('produce_id','>',1)->find();
        $this->find();
    }
    public function insert(){
        $data = ['mobile' => 'bar', 'openid' => 'foo'];
        $resullt = Db::table('user')->insert($data);
        p($resullt);
    }
    public function update(){
        Db::table('user')
            ->where('id', 1)
            ->update([
                'login_time'  => ['exp','now()'],
                'login_times' => ['exp','login_times+1'],
            ]);
        //更新某一个
        Db::table('think_user')
            ->where('id',1)
            ->setField('name', 'thinkphp');
    }
    public function del(){
        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();
    }
    public function find(){
        Db::table('user')
            ->where('name','like','%thinkphp')
            ->where('status',1)
            ->find();
        //SELECT * FROM `user` WHERE `name` LIKE '%thinkphp' AND `status` = 1 LIMIT 1
        Db::table('user')
            ->where('name','like','%thinkphp')
            ->whereOr('openid','like','%thinkphp')
            ->find();
        //SELECT * FROM `user` WHERE `name` LIKE '%thinkphp' OR `openid` LIKE '%thinkphp' LIMIT 1
    }
    public function make(){
        Db::table('user')
            ->where('uid',1)
            ->field('id,name,email')
            ->find();
        Db::table('user')
            ->where('status',1)
            ->where('id',1)
            ->delete();
        Db::listen(function($sql, $time, $explain){
            // 记录SQL
            echo $sql. ' ['.$time.'s]';
            // 查看性能分析结果
            dump($explain);
        });
    }
    public function count(){
        Db::table('think_user')->count();
        Db::table('think_user')->count('id');
        Db::table('think_user')->max('score');
        Db::table('think_user')->where('score>0')->min('score');
        Db::table('think_user')->avg('score');
        Db::table('think_user')->sum('score');
    }
    public function shiwu(){
        // 启动事务
        Db::startTrans();
        try{
            Db::table('think_user')->find(1);
            Db::table('think_user')->delete(1);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }
    public function cache(){
        Cache::set('name',123,3600);
        // name自增(步进值为1)
        Cache::inc('name');
        // name自增(步进值为3)
        Cache::inc('name',3);
        // name自减(步进值为1)
        Cache::dec('name');
        // name自减(步进值为3)
        Cache::dec('name',3);
        //删除缓存
        Cache::rm('name');
        //获取并删除缓存
        Cache::pull('name');
        //不存在则写入缓存数据后返回
        Cache::remember('name',function(){
            return time();
        });
        //缓存标签
        Cache::tag('tag')->set('name1','value1');
        Cache::tag('tag')->set('name2','value2');
        // 或者批量设置缓存标签
        Cache::set('name1','value1');
        Cache::set('name2','value2');
        Cache::tag('tag',['name1','name2']);
        // 清除tag标签的缓存数据
        Cache::clear('tag');
    }
    public function session(){
        Session::set('name','thinkphp');
        Session::get('name');
        // 赋值(当前作用域)
        Session::set('name','thinkphp');
        // 赋值think作用域
        Session::set('name','thinkphp','think');
        // 判断(当前作用域)是否赋值
        Session::has('name');
        // 判断think作用域下面是否赋值
        Session::has('name','think');
        // 取值(当前作用域)
        Session::get('name');
        // 取值think作用域
        Session::get('name','think');
        // 删除(当前作用域)
        Session::delete('name');
        // 删除think作用域下面的值
        Session::delete('name','think');
        // 指定当前作用域
        Session::prefix('think');
    }
    public function cookie(){
        // 设置Cookie 有效期为 3600秒
        Cookie::set('name','value',3600);
        // 设置cookie 前缀为think_
        Cookie::set('name','value',['prefix'=>'think_','expire'=>3600]);
        // 支持数组
        Cookie::set('name',[1,2,3]);
        //判断
        Cookie::has('name');
        // 判断指定前缀的cookie值是否存在
        Cookie::has('name','think_');
        //获取
        Cookie::get('name');
        // 获取指定前缀的cookie值
        Cookie::get('name','think_');
        //删除cookie
        Cookie::delete('name');
        // 删除指定前缀的cookie
        Cookie::delete('name','think_');
    }
    public function file(){
    }
}

下面是模型有关的

 

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
 
namespace app\common\model;
use think\Model;
 
class produce extends Model{
    protected $pk = 'produce_id';
    protected $autoWriteTimestamp = true;//自动写入时间戳
    public function __construct()
    {
        parent::__construct();
    }
    public function index(){
        $user = produce::create([
            'name'  =>  'thinkphp',
            'email' =>  'thinkphp@qq.com'
        ]);
        echo $user->name;
    }
    public function updates(){
        //取出id为1的数据,并且更新后保存
        $user = produce::get(1);
        $user->name     = 'thinkphp';
        $user->email    = 'thinkphp@qq.com';
        $user->save();
 
        $user = new produce;
        // save方法第二个参数为更新条件
        $user->save([
            'name'  => 'thinkphp',
            'email' => 'thinkphp@qq.com'
        ],['id' => 1]);
 
        $user = new produce();
            // 过滤post数组中的非数据表字段数据
        $user->allowField(true)->save($_POST,['id' => 1]);
 
        $user = new produce();
        // post数组中只有name和email字段会写入
        $user->allowField(['name','email'])->save($_POST, ['id' => 1]);
    }
    public function statisUpdate(){
        //模型支持静态方法直接更新数据
        produce::where('id', 1)
            ->update(['name' => 'thinkphp']);
        produce::update(['id' => 1, 'name' => 'thinkphp']);
        //闭包更新
        $user = new produce;
        $user->save(['name' => 'thinkphp'],function($query){
            // 更新status值为1 并且id大于10的数据
            $query->where('status', 1)->where('id', '>', 10);
        });
    }
    public function del(){
        produce::destroy(1);
        // 支持批量删除多个数据
        produce::destroy('1,2,3');
        // 或者
        produce::destroy([1,2,3]);
        // 删除状态为0的数据
        produce::destroy(['status' => 0]);
        //还支持使用闭包删除
        produce::destroy(function($query){
            $query->where('id','>',10);
        });
        //通过数据库类的查询条件删除
        produce::where('id','>',10)->delete();
    }
    public function get_once(){
        //取出主键为1的数据
        $user = produce::get(1);
        echo $user->name;
        // 使用数组查询
        $user = produce::get(['name' => 'thinkphp']);
        // 使用闭包查询
        $user = produce::get(function($query){
            $query->where('name', 'thinkphp');
        });
        echo $user->name;
    }
    public function juhe_find(){
        //静态查询
        produce::count();
        produce::where('status','>',0)->count();
        produce::where('status',1)->avg('score');
        produce::max('score');
        //动态查询
        $user = new produce;
        $user->count();
        $user->where('status','>',0)->count();
        $user->where('status',1)->avg('score');
        $user->max('score');
    }
    //自动更新时间戳
    public function timeSet(){
        $user = new produce();
        $user->name = 'THINKPHP';
        $user->save();
        echo $user->create_time; // 输出类似 1462545582
        echo $user->update_time; // 输出类似 1462545582
    }
    //自动完成
    protected $auto = ['name', 'ip', 'password'];
    protected $insert = ['status' => 1];
    protected $update = [];
    protected function setNameAttr($value)
    {
        return strtolower($value);
    }
    protected function setIpAttr()
    {
        return request()->ip();
    }
    protected function setPasswordAttr($value)
    {
        return md5($value);
    }
    //自动完成结束
    public function autoOver(){
    }
}

任何一个方法使用前,请确保在头部命名空间的地方引入~

 

分享到:更多 ()

相关推荐

  • AI 编辑器 cursor 如何禁止自动更新
  • AI 编辑器 cursor 工具栏改成和 vscode 一样的左侧 竖向展示
  • nodejs 脚本打包为可执行文件
  • 初学 python 笔记
  • nodejs 同时运行多个脚本
  • 让你的照片动起来
  • vue工程项目动态加载umd.js实践
  • 使用 shell 检测目标服务器是否异常
关于我

小天明 北京·朝阳 前端搬砖工程师

碎碎念):(来自公众号)

热门文章

  • 踩坑记录——iphone上safari开启隐身模式时localStorage变为只读-雅荷心语博客踩坑记录——iphone上safari开启隐身模式时localStorage变为只读2017-02-21评论(4)
  • 程序员是怎样一群人-雅荷心语博客程序员是怎样一群人2015-12-08评论(3)
  • 百度你个大毒瘤 - 吐糟博客这几天打不开事情-雅荷心语博客百度你个大毒瘤 – 吐糟博客这几天打不开事情2015-12-28评论(2)
  • PHP 非对称加密 openssl 加密及解密方法-雅荷心语博客PHP 非对称加密 openssl 加密及解密方法2016-05-17评论(2)
  • PHPStorm10 下载安装破解汉化-雅荷心语博客PHPStorm10 下载安装破解汉化2015-12-15评论(2)
2025年8月
一 二 三 四 五 六 日
« 六    
 123
45678910
11121314151617
18192021222324
25262728293031

最新评论

  • 前端小武 9年前 (2017-04-06)说:
    我看到了layer
  • 丁艳平 9年前 (2017-03-03)说:
  • Dawn 9年前 (2016-09-16)说:
    call_user_func_array最后的例子是错哦,你用bc方法去调用类里 另外一个方法就知道问题所在了。情况1.调用非静态方法 第一个参数应该传[类的实例,调用方法] (既然有类实例了直接-&
  • Dawn 9年前 (2016-06-21)说:
    tp框架设置了全局捕获异常的,这也没什么。坑的是 他捕获了异常。然后全部返回404。。。不知道的 还以为自己网站被删除了
  • Dawn 9年前 (2016-05-17)说:
    构造函数里的判断 用异常机制可能更好一些

其他类型

  • 芊云全景
  • 配音兔AI配音神器

博客类型

  • 芊云全景
  • 配音兔AI配音神器

左邻右舍

  • 易水寒
  • 楼教主
  • 芊云全景
  • 贤心
  • 配音兔AI配音神器

雅荷心语博客 -心之所向便是光

联系我们关于我们

© 2025 雅荷心语博客   网站地图