<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## 控制器 控制器主要是用來處理一些頁面的跳轉以及一些邏輯的判斷,不涉及到數據處理,功能都是調用模型類的方法來實現的。 ## 控制器開發 控制器一共有三個,分別是:首頁控制器、欄目控制器、公共部分的控制器。 >[info] 首頁控制器 1、位置位于:```application\index\controller\Index.php``` 2、位置如圖所示: ![](https://box.kancloud.cn/ae289f1f70dd9f8ebfe4c323fa89c6fe_367x200.png) 3、代碼如下: ``` <?php namespace app\index\controller; use think\Controller; /** * 首頁控制器 * Class Index * @package app\index\controller */ class Index extends Controller { /** * 顯示首頁 */ public function index() { return $this->fetch(); } } ``` >[info] 欄目控制器 1、位置位于:```application\index\controller\Cate.php``` 2、位置如圖所示: ![](https://box.kancloud.cn/4b06a0643730c4fa6f52976a8a73e27f_263x199.png) 3、代碼如下: ``` <?php /** * Created by PhpStorm. * User: ye21st * Email: ye21st@gmail.com * Date: 2018/1/23 * Time: 16:33 */ namespace app\index\controller; use think\Controller; use app\index\model\Cate as CateModel; use think\facade\Request; /** * 欄目控制器 * Class Cate * @package app\index\controller */ class Cate extends Controller { /** * 控制器的前置操作 * @var array */ protected $beforeActionList = [ 'delsoncate' => ['only' => 'del'], ]; /** * 欄目列表頁面顯示數據 - 以及,排序功能的寫法 * @return mixed|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function lst() { // 實例化模型 $cate = new CateModel(); // 判斷是否是POST提交數據 if ($this->request->isPost()) { // 獲取post提交過來的數據 $sorts = Request::post(); // 通過ID循環更新排序的值 foreach ($sorts as $k => $v) { $cate->update(['id' => $k, 'sort' => $v]); } $this->success('更新排序成功!', url('index/cate/lst')); return; } // 調用模型的 catetree 拿到數據 $cateres = $cate->catetree(); // 對頁面進行賦值 $this->assign('cateres', $cateres); return $this->fetch(); } public function add() { // 實例化模型 $cate = new CateModel(); // 判斷是否是POST提交數據 if ($this->request->isPost()) { // 獲得post提交過來的數據 $data = Request::post(); // 實例化驗證器 $validate = new \app\index\validate\Cate; // 驗證器驗證 if (!$validate->scene('add')->check($data)) { $this->error($validate->getError()); } // 數據添加 返回 1 或者 0 $add = $cate->save($data); if ($add) { $this->success('添加欄目成功!', url('index/cate/lst')); } else { $this->error('添加欄目失敗!'); } } // 調用模型的 catetree 拿到數據 $cateres = $cate->catetree(); // 對頁面進行賦值 $this->assign('cateres', $cateres); return $this->fetch(); } public function edit() { // 實例化模型 $cate = new CateModel(); // 判斷是否是post提交的數據 if ($this->request->isPost()) { // 獲取POST提交的數據 $data = Request::post(); // 實例化驗證器 $validate = new \app\index\validate\Cate; // 通過驗證器去判斷數據是否合法 if (!$validate->scene('edit')->check($data)) { $this->error($validate->getError()); } // 通過ID修改相應的數據 $save = $cate->save($data, ['id' => $data['id']]); if ($save !== false) { $this->success('修改欄目成功!', url('index/cate/lst')); } else { $this->error('修改欄目失敗!'); } return; } $cates = $cate -> find(Request::param('id')); // 調用自定義模型里面的樹方法 $cateres = $cate->catetree(); // 模板賦值 $this->assign(array( 'cateres' => $cateres, 'cates' => $cates, )); return $this->fetch(); } /** * 欄目刪除操作 * @throws \think\exception\DbException */ public function del() { // 通過ID去進行數據的刪除 $del = CateModel::get(Request::param('id')); $bool = $del->delete(); if ($bool) { $this->success('刪除欄目成功!', url('index/cate/lst')); } else { $this->error('刪除欄目失敗!'); } } /** * 通過要刪除的欄目ID,去循環查出,該欄目下面的子目錄,全部刪除。 * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function delsoncate() { // 要刪除的當前欄目的id $cateid = Request::param('id'); // 實例化模型 $cate = new CateModel(); // 調用模型里面的調用子欄目方法 $sonids = $cate->getchilrenid($cateid); // 獲得全部的欄目ID $allcateid = $sonids; $allcateid[] = $cateid; if ($sonids) { $cate->delete($sonids); } } } ``` >[info] 公共部分控制器 1、位置位于:```application\index\controller\Common.php``` 2、位置如圖所示: ![](https://box.kancloud.cn/3e1948f1f8155a0a16ebe7ba0f517b4e_269x197.png) 3、代碼如下: ``` <?php /** * Created by PhpStorm. * User: ye21st * Email: ye21st@gmail.com * Date: 2018/1/23 * Time: 15:36 */ namespace app\index\controller; use think\Controller; /** * 公共控制器 - 主要用來存放一些公共的頁面,比如,頭部、底部 * Class Common * @package app\index\controller */ class Common extends Controller { /** * 頭部控制器 - 顯示頁面 */ public function header(){ $this->fetch(); } /** * 底部控制器 - 顯示頁面 */ public function footer(){ $this->fetch(); } } ``` >[success] 至此,控制器開發也就完成了,到了這里,程序應該就可以正常跑起來了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看