Blogger Information
Blog 119
fans 3
comment 1
visits 94649
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
TP6 MVC 基础、中间键
赵大叔
Original
999 people have browsed it

TP6 MVC 基础


Model:模型类,数据库操作,返回数据

  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\index\model;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class Getuser extends Model
  9. {
  10. protected $name = 'users';
  11. //
  12. public function getUser ()
  13. {
  14. return Getuser::select()->toArray();
  15. }
  16. }

View:视图类,展示到客户端

  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\index\controller;
  4. use app\index\model\Getuser as userModel;
  5. class Getuser
  6. {
  7. public function index(userModel $user)
  8. {
  9. $data['res'] = $user->getUser();
  10. /* echo '<pre>';
  11. print_r($res); */
  12. return view('cats/cats',$data);
  13. }
  14. }

数据渲染:
[http://help10086.cn/index/getuser]

Controller:控制器,协调模型与视图,在控制器中使用模型:引入Model类,调用Model方法

  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\index\controller;
  4. use app\index\model\Getuser as userModel;
  5. class Getuser
  6. {
  7. public function index(userModel $user)
  8. {
  9. // 调用`Model方法`
  10. $data['res'] = $user->getUser();
  11. /* echo '<pre>';
  12. print_r($res); */
  13. return view('cats/cats',$data);
  14. }
  15. }

TP6 多运用中间键

  • 创建:middleware 目录下创建中间键类
  • 必须要有return $next($request);代码才能往下继续执行
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\index\middleware;
  4. class Check
  5. {
  6. /**
  7. * 处理请求
  8. *
  9. * @param \think\Request $request
  10. * @param \Closure $next
  11. * @return Response
  12. */
  13. public function handle($request, \Closure $next)
  14. {
  15. if (empty($request->session('username'))) {
  16. return redirect('/index/login');
  17. }
  18. return $next($request);
  19. }
  20. }
  • 注册:在运用中,config 目录下 middleware.php 文件中使用别名注册
  1. <?php
  2. // 中间件配置
  3. return [
  4. // 别名或分组
  5. 'alias' => [
  6. 'checkLogin' => app\index\middleware\Check::class,
  7. ],
  8. // 优先级设置,此数组中的中间件会按照数组中的顺序优先执行
  9. 'priority' => [],
  10. ];
  • 调用:路由分组调用
  1. Route::group(function(){
  2. Route::get('/', 'index/index');
  3. Route::get('home','account/home');
  4. Route::get('logout', 'account/logout');
  5. })->middleware('checkLogin');

登陆验证(测试user:admin,密码:123456):
[http://help10086.cn/index/login]

关联查询

  • 外键:另外一张数据表的主键
  • 关联查询多用于一对多关系的查询
  1. Db::table('think_artist')
  2. ->alias('a')
  3. ->field(a.username,u.waimai,w.username,w,province)
  4. // 下面是子表外键管理的外表数据库字段,比如我a表id关联了w表的uid,下面就应该这么些
  5. ->join('work w','a.id = w.uid')
  6. ->select();
  7. Db::table('要查询的主表名')
  8. ->alias('主表别名')
  9. // field查询的字段
  10. ->field(主表别名.查询的字段,子表别名.字表查询的字段)
  11. // 下面是子表外键管理的外表数据库字段,比如我a表id关联了w表的uid,下面就应该这么些,前面是子表的名称 后面是子表的别名,然后后面是主表别名.主表字段=(等于也就是关联)的子表别名.子表字段,也就是主表字段关联的子表字段。
  12. ->join('子表名 子表名的别名','主表别名.主表字段 = 子表别名.子表字段')
  13. ->select();
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