abstract:<?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/26 * Time: 8:11 */ namespace app\admin\controller; us
<?php namespace app\admin\controller; use think\facade\Request; use think\facade\Session; use app\admin\model\NewsModel; use app\admin\controller\Common; class News extends Common { // 新闻列表 public function index() { // 获取新闻列表 $news = NewsModel::order('id','desc')->paginate(8); // 模板赋值 $this->assign('news',$news); // 渲染模板 return $this->fetch(); } // 添加新闻页面 public function add() { // 渲染模板 return $this->fetch(); } // 添加操作 public function DoAdd() { // 获取表单信息 $data = Request::param(); $data['time'] = time(); $data['username'] = Session::get('username'); $title = $data['title']; // 检测新闻标题是否存在 $news = NewsModel::where('title',$title)->find(); if($news) { return ['code'=>0,'msg'=>'新闻标题重复']; } // 写入数据库中 if(NewsModel::create($data)) { return ['code'=>1,'msg'=>'发布成功']; } // 发布失败 return ['code'=>0,'msg'=>'发布失败']; } // 文件上传 public function upload() { // 获取上传的图片信息 $file = Request::file('img'); // 将文件上传移动到指定目录 if($info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload')) { // 返回上传成功的信息 return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]); } else { // 返回错误的信息 return $file->getError(); } } // 编辑页面 public function edit() { // 获取新闻ID $newId = Request::param('id'); // 根据新闻ID查找对应的内容数据 $news = NewsModel::get($newId); // 个模板赋值 $this->assign('news',$news); // 渲染编辑页面 return $this->fetch(); } // 修改编辑 public function DoEdit() { // 获取上传的数据 $data = Request::param(); // 更新条件 $newId = $data['id']; // 更新数据 $res = NewsModel::where('id',$newId)->update([ 'title'=>$data['title'], 'desc' =>$data['desc'], 'content'=>$data['content'], 'username'=>Session::get('username'), 'time'=>time(), ]); if($res) { // 返回更新成功 return ['code'=>1,'msg'=>'数据更新成功']; } // 返回更新失败 return ['code'=>0,'msg'=>'数据更新失败']; } // 删除操作 public function DoDel() { $newId = Request::param('id'); // 删除数据 使用软删除 $res = NewsModel::destroy($newId); if($res) { return ['code'=>1,'msg'=>'数据删除成功']; } return ['code'=>0,'msg'=>'数据删除失败']; } }
Correcting teacher:天蓬老师Correction time:2019-04-26 13:36:43
Teacher's summary:框架的代码,比较规范, 不错, 就是方法的头部注释加上会更好