Correction status:Uncorrected
Teacher's comments:
完成模型中的CURD操作,全部采用静态方法;
<?php namespace app\index\controller; use app\model\User; use traits\controller\Jump; class Demo2 { use Jump; public function getOne(){ // $res = User::get(1); // dump($res); $salary = 5000; $res = User::all(function ($query)use ($salary){ $query->where('age','>',25) ->where('salary','>',$salary); }); foreach ($res as $user){ echo $user->name,'->',$user->salary,'<br>'; } } public function getAll(){ $salary = 5000; $res = User::all(function ($query)use ($salary){ $query->where('age','>',25) ->where('salary','>',$salary); }); dump($res); } public function update(){ User::update(['age'=>33],['id'=>4]); $this->success('更新成功','show'); } public function show(){ dump(User::where('id',4)->field('id,age,name')->select()); } }
点击 "运行实例" 按钮查看在线实例
练习视图中的assign()和fetch()方法
<?php namespace app\index\controller; use think\facade\View; use app\model\User; class Demo3 { public function index(){ $site = '小渣渣'; $user = User::get(4); $list = User::all(); View::assign('lee',$site); View::assign('user',$user); View::assign('list',$list); return View::fetch('index'); } }
点击 "运行实例" 按钮查看在线实例
试着找一个后台模块,并将首页导入到框架项目中。
<?php namespace app\index\controller; use think\facade\View; class Index { public function index() { return View::fetch('index'); } public function welcome(){ return View::fetch('welcome'); } public function hello($name = 'ThinkPHP5') { return 'hello,' . $name; } }
点击 "运行实例" 按钮查看在线实例
总结,后台已完成,感觉有模版,代码非常容易!