摘要:<?phpnamespace app\admin\controller;use app\admin\model\UserModel;use think\Controller;use think\facade\Request;class User extends Controller{ public function index() { $user = new Us
<?php
namespace app\admin\controller;
use app\admin\model\UserModel;
use think\Controller;
use think\facade\Request;
class User extends Controller
{
public function index()
{
$user = new UserModel();
//按ID降序排序,并且每一頁(yè)設(shè)為八條數(shù)據(jù)
$users = $user->order('id','desc')->paginate(8);
$this->view->users = $users;
//渲染管理員界面
return $this->fetch();
}
//渲染管理員添加界面
public function add()
{
return $this->fetch();
}
public function DoAdd()
{
//獲取前臺(tái)提交過(guò)來(lái)的數(shù)據(jù)
$data = Request::param();
//獲取添加的時(shí)間
$data['time'] = time();
$username = $data['username'];
//使用用戶名來(lái)查詢數(shù)據(jù)庫(kù)是否有對(duì)應(yīng)的數(shù)據(jù)
$res = UserModel::where('username',$username)->find();
//判斷數(shù)據(jù)是否已經(jīng)存在
if($res == true){
return['res'=>0,'msg'=>'用戶名已存在!'];
}
//實(shí)例化模型
$user = new UserModel();
// 驗(yàn)證數(shù)據(jù)存入數(shù)據(jù)庫(kù)
if($user->save($data)){
return ['res'=>1,'msg'=>'添加成功!'];
}else{
return ['res'=>0,'msg'=>'添加失敗!'];
}
}
public function edit()
{
//獲取前臺(tái)提交過(guò)來(lái)的數(shù)據(jù)
$userId = Request::param('id');
//通過(guò)用戶id查詢需要更新用戶的所有數(shù)據(jù)
$user = UserModel::get($userId);
//將數(shù)據(jù)賦值給模板
$this->view->user = $user;
//渲染編輯頁(yè)面
return $this->fetch();
}
public function DoEdit()
{
$data = Request::param();
$user = new UserModel();
$res = $user->save([
'username'=>$data['username'],
'time'=>time(),
],['id'=>$data['id']]);
if($res){
return ['res'=>1,'msg'=>'修改成功!'];
}
}
public function del()
{
//獲取需要?jiǎng)h除管理員的id
$userId = Request::param('id');
//實(shí)例化模型
$user = new UserModel();
//進(jìn)行刪除并驗(yàn)證操作
if($user->destroy($userId)){
return ['res'=>1,'msg'=>'刪除成功!'];
}
}
}
批改老師:天蓬老師批改時(shí)間:2019-05-30 15:58:29
老師總結(jié):public function DoEdit(), 下次不要這樣命名, 應(yīng)該用doEdit