批改状态:合格
老师批语:

Model:模型类,数据库操作,返回数据
<?phpdeclare (strict_types = 1);namespace app\index\model;use think\Model;/*** @mixin \think\Model*/class Getuser extends Model{protected $name = 'users';//public function getUser (){return Getuser::select()->toArray();}}
View:视图类,展示到客户端
<?phpdeclare (strict_types = 1);namespace app\index\controller;use app\index\model\Getuser as userModel;class Getuser{public function index(userModel $user){$data['res'] = $user->getUser();/* echo '<pre>';print_r($res); */return view('cats/cats',$data);}}
数据渲染:
[http://help10086.cn/index/getuser]
Controller:控制器,协调模型与视图,在控制器中使用模型:引入Model类,调用Model方法
<?phpdeclare (strict_types = 1);namespace app\index\controller;use app\index\model\Getuser as userModel;class Getuser{public function index(userModel $user){// 调用`Model方法`$data['res'] = $user->getUser();/* echo '<pre>';print_r($res); */return view('cats/cats',$data);}}
return $next($request);代码才能往下继续执行
<?phpdeclare (strict_types = 1);namespace app\index\middleware;class Check{/*** 处理请求** @param \think\Request $request* @param \Closure $next* @return Response*/public function handle($request, \Closure $next){if (empty($request->session('username'))) {return redirect('/index/login');}return $next($request);}}
<?php// 中间件配置return [// 别名或分组'alias' => ['checkLogin' => app\index\middleware\Check::class,],// 优先级设置,此数组中的中间件会按照数组中的顺序优先执行'priority' => [],];
Route::group(function(){Route::get('/', 'index/index');Route::get('home','account/home');Route::get('logout', 'account/logout');})->middleware('checkLogin');
登陆验证(测试user:admin,密码:123456):
[http://help10086.cn/index/login]
Db::table('think_artist')->alias('a')->field(a.username,u.waimai,w.username,w,province)// 下面是子表外键管理的外表数据库字段,比如我a表id关联了w表的uid,下面就应该这么些->join('work w','a.id = w.uid')->select();Db::table('要查询的主表名')->alias('主表别名')// field查询的字段->field(主表别名.查询的字段,子表别名.字表查询的字段)// 下面是子表外键管理的外表数据库字段,比如我a表id关联了w表的uid,下面就应该这么些,前面是子表的名称 后面是子表的别名,然后后面是主表别名.主表字段=(等于也就是关联)的子表别名.子表字段,也就是主表字段关联的子表字段。->join('子表名 子表名的别名','主表别名.主表字段 = 子表别名.子表字段')->select();
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号