摘要:<?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();
}
}
//添加页面数据处理;
public function doAdd(){
//获取请求添加的信息;
$data = Request::param();
$data['time'] = time();
$data['username'] = Session::get('username');
$title = $data['title'];
$new = NewModel::where('title', "$title")->find();
//判断这个标题存在就报错;
if($new){
return ['code'=>0,'msg'=>'标题已存在'];
}
$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函数,直接快捷回去一整条数据;
$user = NewModel::get($id);
//模板赋值;
$this->view->assign('user',$user);
return $this->fetch();
}
//处理编辑数据;
public function doEdit(){
//获取前台提交的数据;
$data = Request::param();
//将获取到的数据放到数据库进行修改;
$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'=>'删除失败'];
}
}
}
批改老师:查无此人批改时间:2019-06-05 09:22:57
老师总结:完成的不错。后台cms管理系统,就是对数据进行操作。操作越简单越好。继续加油。