サマリー:class NewsPic extends Common{ public function index() { //實(shí)例化模型 $newPic = new NewsPicModel();  
class NewsPic extends Common
{
public function index()
{
//實(shí)例化模型
$newPic = new NewsPicModel();
$pics = $newPic->order('id', 'desc')->paginate(6);
$this->view->pics = $pics;
// 渲染新聞縮略圖列表
return $this->fetch();
}
public function add()
{
// 查詢(xún)所有新聞數(shù)據(jù)
$news = NewsModel::all();
//將數(shù)據(jù)賦值給模板
$this->view->news = $news;
// 渲染新聞縮略圖添加界面
return $this->fetch();
}
public function upload()
{
// 獲取上傳圖片的信息
$file = Request::file('file');
// 驗(yàn)證圖片并移動(dòng)到指定目錄
if ($info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload')) {
// 拼接圖片路徑
$fileName = '/upload/' . $info->getSaveName();
// 返回上傳成功的提示信息
return json([1, '上傳成功!', 'data' => $fileName]);
} else {
// 返回上傳失敗的錯(cuò)誤信息
return $file->getError();
}
}
public function DoAdd()
{
// 獲取提交的數(shù)據(jù)
$data = Request::param();
$data['time'] = time();
$data['username'] = Session::get('username');
$newPic = new NewsPicModel();
if ($newPic->save($data)) {
return ['res' => 1, 'msg' => '發(fā)布成功!'];
} else {
return ['res' => 0, 'msg' => '發(fā)布失??!'];
}
}
public function del()
{
$picId=Request::param('id');
$newPic = new NewsPicModel();
if ($newPic->destroy($picId)){
return ['res'=>1];
}
添削の先生:天蓬老師添削時(shí)間:2019-03-30 10:24:03
先生のまとめ:對(duì)于控制器來(lái)說(shuō), 最常用的就是CURD, 其實(shí)框架還提供了命令的, 可以一條指令生成主要的CURD代碼,可以查閱手冊(cè)