サマリー:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/5/14 * Time: 15:09 */ namespace app\admin\contro
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/5/14 * Time: 15:09 */ namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\SlideModel; use think\facade\Request; use think\facade\Session; class slide extends Common { public function index() { // 實(shí)例化模型 $slide = new SlideModel(); // 查詢(xún)數(shù)據(jù)按照id排序并且每頁(yè)四條數(shù)據(jù) $slides = $slide->order('id', 'desc')->paginate(4); // 將數(shù)據(jù)賦值到模型 $this->view->slides = $slides; // 渲染模板 return $this->fetch(); } public function add() { // 渲染模板 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() { // 獲取提交過(guò)來(lái)的數(shù)據(jù) $data = Request::param(); // 加入添加時(shí)間 $data['time'] = time(); // 加入添加管理員 $data['username'] = Session::get('username'); // 實(shí)例化模型 $slide = new SlideModel(); // 存儲(chǔ)并驗(yàn)證 if ($slide->save($data)) { // 返回對(duì)應(yīng)信息 return ['res' => 1, 'msg' => '添加成功!']; } else { return ['res' => 0, 'msg' => '添加失??!']; } } public function del() { // 獲取需要?jiǎng)h除的id $slideId = Request::param('id'); // 實(shí)例化模型 $slide = new SlideModel(); // 刪除并驗(yàn)證 if ($slide->destroy($slideId)) { return ['res' => 1]; } } }
添削の先生:天蓬老師添削時(shí)間:2019-05-14 17:45:45
先生のまとめ:// 實(shí)例化模型
$slide = new SlideModel();
推薦依賴(lài)注入, 而不是在類(lèi)方法中實(shí)例化