abstract:<?php namespace app\admin\controller; use app\admin\model\NewModel; use app\admin\controller\Common; use think\facade\Request; use think\facade\Session; class News&
<?php namespace app\admin\controller; use app\admin\model\NewModel; use app\admin\controller\Common; use think\facade\Request; use think\facade\Session; class News extends Common { //顯示主頁面; public function index(){ // $news = NewModel::all(function($query){ // $query->field('*'); // }); $news = NewModel::field('*')->paginate(1); $this->view->assign('news',$news); return $this->fetch(); } //顯示添加頁面 public function add(){ return $this->fetch(); } //文件上傳; public function upload(){ //獲取上傳的圖片; $file = Request::file('file'); // 將圖片移動到upload文件下面 $info = $file->validate(['ext'=>'jpg,png,jpeg,gif'])->move('upload'); //判斷文件是否存在; if($info){ //獲取路徑; return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]); }else{ return $file->getError(); } } //添加頁面數(shù)據(jù)處理; public function doAdd(){ //獲取請求添加的信息; $data = Request::param(); $data['time'] = time(); $data['username'] = Session::get('username'); $title = $data['title']; $new = NewModel::where('title', "$title")->find(); //判斷這個標(biāo)題存在就報錯; if($new){ return ['code'=>0,'msg'=>'標(biāo)題已存在']; } $News = new NewModel(); if($News->save($data)){ return ['code'=>1,'msg'=>'添加成功']; }else{ return ['code'=>0,'msg'=>'添加失敗']; } } //顯示編輯頁面 public function edit(){ //請求傳過來的id; 在視圖中必須加一個隱藏的input輸入id; $id = Request::param('id'); //用get函數(shù),直接快捷回去一整條數(shù)據(jù); $user = NewModel::get($id); //模板賦值; $this->view->assign('user',$user); return $this->fetch(); } //處理編輯數(shù)據(jù); public function doEdit(){ //獲取前臺提交的數(shù)據(jù); $data = Request::param(); //將獲取到的數(shù)據(jù)放到數(shù)據(jù)庫進(jìn)行修改; $news = new NewModel(); // save('修改的字段和值','需要修改的條件'); $res = $news->save([ 'title'=>$data['title'], 'desc'=>$data['desc'], 'content'=>$data['content'], 'username'=>Session::get('username'), 'time'=>time() ],['id'=>$data['id']]); if($res){ return['code'=>0,'msg'=>'修改成功']; }else{ return['code'=>1,'msg'=>'修改失敗']; } } //刪除操作; public function del(){ $newId = Request::param('id'); $new = new NewModel(); $res = $new->destroy($newId); if($res){ return['code'=>1,'msg'=>'刪除成功']; }else{ return['code'=>0,'msg'=>'刪除失敗']; } } }
Correcting teacher:查無此人Correction time:2019-06-05 09:22:57
Teacher's summary:完成的不錯。后臺cms管理系統(tǒng),就是對數(shù)據(jù)進(jìn)行操作。操作越簡單越好。繼續(xù)加油。