abstrak:<?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降序排序,并且每一頁設(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()
{
//獲取前臺提交過來的數(shù)據(jù)
$data = Request::param();
//獲取添加的時間
$data['time'] = time();
$username = $data['username'];
//使用用戶名來查詢數(shù)據(jù)庫是否有對應(yīng)的數(shù)據(jù)
$res = UserModel::where('username',$username)->find();
//判斷數(shù)據(jù)是否已經(jīng)存在
if($res == true){
return['res'=>0,'msg'=>'用戶名已存在!'];
}
//實例化模型
$user = new UserModel();
// 驗證數(shù)據(jù)存入數(shù)據(jù)庫
if($user->save($data)){
return ['res'=>1,'msg'=>'添加成功!'];
}else{
return ['res'=>0,'msg'=>'添加失敗!'];
}
}
public function edit()
{
//獲取前臺提交過來的數(shù)據(jù)
$userId = Request::param('id');
//通過用戶id查詢需要更新用戶的所有數(shù)據(jù)
$user = UserModel::get($userId);
//將數(shù)據(jù)賦值給模板
$this->view->user = $user;
//渲染編輯頁面
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()
{
//獲取需要刪除管理員的id
$userId = Request::param('id');
//實例化模型
$user = new UserModel();
//進行刪除并驗證操作
if($user->destroy($userId)){
return ['res'=>1,'msg'=>'刪除成功!'];
}
}
}
Guru membetulkan:天蓬老師Masa pembetulan:2019-05-30 15:58:29
Rumusan guru:public function DoEdit(), 下次不要這樣命名, 應(yīng)該用doEdit