abstract:<?php/** * Created by PhpStorm. * User: Administrator * Date: 2019-01-28 * Time: 9:18 */namespace app\admin\controller;use app\admin\controller\Common;use think\facade\Request;use  
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019-01-28
* Time: 9:18
*/
namespace app\admin\controller;
use app\admin\controller\Common;
use think\facade\Request;
use think\facade\Session;
use app\admin\model\NewsModel;
class News extends Common
{
public function index()
{
$news =new NewsModel();
$new = $news ->order('id','desc')->paginate(5);
$this->view->new = $new;
return $this->fetch();
}
public function add() //新聞加載
{
return $this->fetch();
}
public function edit() //新聞編輯
{
$newid= Request::param('id');
$new = NewsModel::get($newid);
$this->view->new =$new;
return $this->fetch();
}
public function upload()
{
$file = Request::file('img');
$info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload');
if($info)
{
//上傳成功信息
return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]);
}
else
{
//返回錯誤信息
return $file->getError();
}
}
public function doadd() //新增
{
//獲取數(shù)據(jù)
$data = Request::param();
//獲取時間
$data['time']= time();
//獲取操作員
$data['username']=Session::get('username');
//取出標題
$title= $data['title'];
//查數(shù)據(jù)庫是否有
$news = NewsModel::where('title',"$title")->find();
//對比
if ($news== true)
{
return ['res'=>0,'msg'=>'新聞名稱重復'];
}
//實例化
$new= new NewsModel();
//保存
$res = $new->save($data);
//檢查結(jié)果
if ($res== true)
{
return ['res'=>1,'msg'=>'發(fā)布成功'];
}
else
{
return['res'=>0, 'msg'=>'發(fā)布失敗'];
}
}
public function doedit() //編輯
{
//
$data=Request::param('');
//
$new = new NewsModel();
$res= $new ->save([
'title'=>$data['title'],
'desc'=>$data['desc'],
'content'=>$data['content'],
'time'=>time(),
],['id'=>$data['id']]
);
if($res)
{
return ['res'=>1,'msg'=>'修改成功'];
}
else
{
return['res'=>0,'msg'=>'修改失敗'];
}
}
public function del()
{
$newid= Request::param('id');
$new=new NewsModel();
$res = $new->destroy($newid);
if ($res)
{
return ['res'=>1,'msg'=>'刪除成功'];
}
else
{
return['res'=>0, 'msg'=>'刪除失敗'];
}
}
}
Correcting teacher:天蓬老師Correction time:2019-01-30 09:30:55
Teacher's summary:if ($res)
{
return ['res'=>1,'msg'=>'刪除成功'];
}
else
{
return['res'=>0, 'msg'=>'刪除失敗'];
}
在一個函數(shù)中出現(xiàn)多個return , 并不是一個好的寫法, 在一些高級編輯器中, 會