Blogger Information
Blog 46
fans 2
comment 0
visits 19141
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1、model 搜索器 2、门面类 3、多应用 4、调试 5、助手函数
P粉314265155
Original
523 people have browsed it

1、model 搜索器 2、门面类 3、多应用 4、调试 5、助手函数

  1. public function three (){
  2. // print_r(Request::post());
  3. // 1、method 请求方法 获取当前请求的类型
  4. // print_r(Request::method());
  5. // PHP做接口 给vue 小程序 app,保证数据安全,必须放弃get传值方式 get传值方式在 浏览器上
  6. // 判断 请求方式
  7. // if(Request::method() != 'POST'){
  8. // echo '请用post请求';
  9. // exit;
  10. // }
  11. // echo 'post提交后返回数据';
  12. // exit;
  13. // 2、 isGet isPost
  14. // if(Request::isPost()){
  15. // echo 'post请求';
  16. // }else{
  17. // echo 'get请求';
  18. // }
  19. // print_r(Request::request());
  20. // 全局环境变量
  21. // print_r(Request::server());
  22. // echo time();
  23. // echo '<hr>';
  24. // echo Request::server()['REQUEST_TIME'];
  25. // 3、联表查询 统计数据 时需要
  26. // join 链式操作 任何操作 都要增加表的名称,因为是联表查询 可以给表起别名
  27. // $user =Db::table('lh_user') ->join('lh_order','lh_user.uid=lh_order.uid')->select();
  28. // print_r($user);
  29. // 起别名 注意中间空一格空格 不能空两格空格
  30. // $user =Db::table('lh_user u') ->join('lh_order o','u.uid = o.uid')->select();
  31. // 另外一种起别名的方式 alias join 可以一直加
  32. // $user =Db::table('lh_user') ->alias('u')->join('lh_order o','u.uid = o.uid')->select();
  33. // print_r($user);
  34. // INNER JOIN: 等同于 JOIN(默认的JOIN类型),如果表中有至少一个匹配,则返回行
  35. // LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
  36. // RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
  37. // FULL JOIN: 只要其中一个表中存在匹配,就返回行
  38. // $user =Db::table('lh_user') ->alias('u')->leftJoin('lh_order o','u.uid = o.uid')->select();
  39. // print_r($user);
  40. // 使用模型。先引入 app\model\LhUser
  41. // $LhUser = new LhUser();
  42. // $LhUser -> lists();
  43. // $User = new User();
  44. // $User -> lists();
  45. // $User = new User();
  46. // $User -> one();
  47. // $User = new User();
  48. // $data = [
  49. // 'name' => '小心',
  50. // 'password' =>123,
  51. // 'email' =>'1232@qq.com',
  52. // 'register_time' =>'123456780',
  53. // ];
  54. // $User -> insert($data);
  55. // $user =new User();
  56. // $user ->sele();
  57. // $user =new User();
  58. // $sele = $user ->sele();
  59. // print_r($sele);
  60. }
  61. // 注入的方式把普通类 放进方法中 但是每次都要以依赖注入的方式
  62. // 多个时
  63. // public function four (Db $db , Requset $request){
  64. // public function four (Db $db){
  65. // $select = $db ->table('lh_user')->select();
  66. // print_r($select);
  67. // }
  68. // 门面类 获取get传值
  69. // public function five (){
  70. // print_r(Request::get());
  71. // // tp 函数获取get传值
  72. // input('get.');
  73. // input('post.');
  74. // print_r(input('get.'));
  75. // print_r(input('post.'));
  76. // print_r(config());
  77. // // 5、注意 门面类 与普通类 助手函数一些功能是重复的
  78. // }
  79. // public function six(){
  80. // 助手函数
  81. // $user = Db::table('lh_user')->select();
  82. // dump($user);
  83. // }
  84. // 6、多应用http://bite.com/Index.php/index/index/three
  85. // 单应用http://bite.com/Index.php/index/three
  86. // 7、php原生循环 在html 中进行的循环
  87. // <?php
  88. // foreach ($arr as $a ){}
  89. // /?/ >
  90. // 视图引入门面类
  91. // think\facade\View
  92. public function for(){
  93. // echo 111;
  94. // mvc v放在项目里面 index目录
  95. // php 文件名 跟class名对应,代表V的目录
  96. // php方法名 对应v的目录下的index文件下的for方法名 重点 关注 否则无法通过c 访问v视图
  97. echo '11';
  98. $user = Db::table('lh_user')->where('status',1)->select();
  99. foreach($user as $v){
  100. // echo $v['name'].'<br>';
  101. echo '<li style="color:green;">' .$v['name'] . '</li>';
  102. }
  103. // 在c里面获取的数据,要传给v,才能在页面中进行设置,进行循环
  104. // 在c里,我们只返回数据,样式在v里写, m里写对数据库表的操作
  105. // print_r($user);
  106. // 模板变量赋值 key 值
  107. // user 1 给到 视图 里面的$user1 $user 给到 c里面的 user1
  108. View::assign('user1',$user);
  109. return View::fetch();
  110. }

model

  1. <?php
  2. // 模型文件
  3. namespace app\index\model;
  4. use think\Model;
  5. class User extends Model
  6. {
  7. // 设置当前模型对应的完整数据表名称
  8. // protected $table = 'lh_user';
  9. // 设置当前模型的数据库连接
  10. // protected $connection = 'db_config';
  11. // 设置废弃的字段 查询结果就看不到了
  12. // protected $discuse = [
  13. // 'name',
  14. // 'phone'
  15. // ];
  16. // 5 、 数据类型是 php 的类型
  17. // protected $type = [
  18. // 'uid' => 'int',
  19. // 'name' => 'string',
  20. // ];
  21. // protected $pk = 'uid';
  22. // protected $name = 'user';
  23. // public function lists(){
  24. // Db:: 在控制器contraller 用
  25. // 模型里面用 自己的class名称
  26. // 1、select 是tp 自带的方法 在每个model 模型都能直接使用 在任意表中使用
  27. // $ret = User::select();
  28. // print_r($ret);
  29. // 2、 默认 主键为 id 如果是uid查询报错,因此需要增加 where条件
  30. // 可以把主键从id 改为 uid 就不用 where protected $pk = 'uid'; https://www.kancloud.cn/manual/thinkphp6_0/1037580
  31. // 因此 是where 和find 都是可用的 返回的是对象
  32. // 所有的链式操作都是可以用的
  33. // $ret = LhUser::where('uid',1)->find();
  34. // $ret = LhUser::find(1);
  35. // print_r($ret);
  36. // 3、结果跟 controller 用 门面 db 是一样的
  37. // print_r($ret['name']);
  38. // 4、学过的 数据处理都是可以转为 toArray 从对象转为 数组
  39. // print_r($ret ->toArray());
  40. // foreach ($ret as $ret_s){
  41. // if( $ret_s['status'] == 1){
  42. // $ret_s['status']= '开启';
  43. // }else{
  44. // $ret_s['status']= '关闭';
  45. // }
  46. // }
  47. // print_r($ret ->toArray());
  48. // }
  49. // public function One(){
  50. // $ret = User::find(4);
  51. // print_r($ret);
  52. // }
  53. // 6、获取器 格式 :get +字段 +Attr 创建一个方法名
  54. // 6.1 会接受一个参数
  55. // public function getStatusAttr($v){
  56. // 6.2 所有的状态都会传到这个方法里面
  57. // print_r($v);
  58. // if ($v==0){
  59. // $ret ='开启';
  60. // }else{
  61. // $ret ='关闭';
  62. // }
  63. // 这里要return 回去 给查询的那条语句
  64. // return $ret;
  65. // 直接返回 没有把v 使用
  66. // $arr = [
  67. // 0 => '禁用',
  68. // 1 =>'启用',
  69. // 2 =>'待审核',
  70. // ];
  71. // return $arr[$v];
  72. // }
  73. // public function insert($data){
  74. // $ret = User::create($data);
  75. // 注意数据结果 也是要返回,model里面的任何对外的方法 ,都要用return f返回
  76. // return $ret;
  77. // }
  78. // 7、 修改器 :格式 :get +字段 +Attr 创建一个方法名
  79. // 7.1会接受两个传值 1个是 字段值 2全不值
  80. // public function setPasswordAttr($v,$all){
  81. // print_r($v);
  82. // print_r($all);
  83. // return md5($v);
  84. // }
  85. // public function sele(){
  86. // User::withSearch(['name','create_time'], [
  87. // 'name' => 'think',
  88. // 'create_time' => ['2018-8-1','2018-8-5'],
  89. // 'status' => 1
  90. // ])
  91. // ->select();
  92. // withSearch 三个参数 一个 是 字段名 是一个是区间 、 范围、值 第三个比如 传多值就需要第三个参数
  93. // $lists = User::withSearch(
  94. // ['name'],
  95. // ['name' =>'心'],
  96. // ) ->select();
  97. // // 模型里面的方法都需要 retrun 返回自己不做打印输出
  98. // return $lists;
  99. // }
  100. // 1、搜索器 search +字段名 +Attr
  101. // 两个参数 one 是整个查询语句 结果是对象 two搜索的值
  102. // public function searchNameAttr($one,$two){
  103. // print_r($one);
  104. // print_r($two);
  105. // exit;
  106. // $one -> where('name','like','%'.$two.'%');
  107. // }
  108. // 查询后 执行 比如 知道谁操作了数据库 记录日志
  109. // 事件会在新增操作和更新操作都会触发.
  110. // 具体的触发顺序:
  111. // 执行 onBeforeWrite
  112. // 如果事件没有返回`false`,那么继续执行
  113. // 执行新增或更新操作(onBeforeInsert/onAfterInsert或onBeforeUpdate/onAfterUpdate)
  114. // 新增或更新执行成功
  115. // 执行 onAfterWrite
  116. // public static function onAfterRead($u)
  117. // {
  118. // print_r($u);
  119. // $data1 = [
  120. // 'add_time' => time(),
  121. // 'info' => '查询的数据是'.json_encode($u),
  122. // 前后端分离使用token 加密方式传值,前后端不分离 使用session跟cookie 记录
  123. // ];
  124. // print_r($data1);
  125. // }
  126. // 杂项
  127. // 缓存 config/cache.php 文件
  128. }

多应用

  1. https://www.kancloud.cn/manual/thinkphp6_0/1037484
  2. composer require topthink/think-multi-app

调试

  1. https://www.kancloud.cn/manual/thinkphp6_0/1037618
  2. .env文件的定义格式如下:
  3. // 设置开启调试模式
  4. APP_DEBUG = true
  5. // 其它的环境变量设置
  6. // ...

助手函数

  1. https://www.kancloud.cn/manual/thinkphp6_0/1037653

视图

  1. composer require topthink/think-view
  2. https://www.kancloud.cn/manual/thinkphp6_0/1037608
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!