<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>

                # (3)MVC架構 #### 1. 命名空間 在序言中就已經提到MVC架構,<http://www.hmoore.net/manual/thinkphp5/122950> 官方是這么解釋的,但是這一切都必須建立在命名空間的基礎之上,這是官方解釋的命名空間http://www.hmoore.net/manual/thinkphp5/118014 ,點到為止,不清楚可以自行腦補。 #### 2. 新建控制器接收數據 在上一節中已經新建好了兩個控制器,Admin.php跟Login.php,這里就繼續在上面擴展: 下面是Login.php代碼: ``` <?php namespace app\index\controller; use think\Input; use think\Controller; use Captcha; use think\View; class Login extends Controller { public function login(){ return $this->fetch(); } public function logining() { $name = input('request.name'); $password = input('request.password'); $data = input('request.captcha'); // dump($data); if(!captcha_check($data)){ //驗證失敗 return $this->error("驗證碼錯誤","Login/login"); }; // $check=\app\index\model\Admin::login($name, $password); // if ($check) { header(strtolower("location:". config("web") . "index/admin/admin")); exit(); // }else{ // return $this->error("用戶名或密碼錯誤","Login/login"); //} } } ``` 這個是我提前寫好一次性復制上來的,看到這個config("web"),所以還需要在配置文件中新增一條: ``` return [ "web_root" => "/tp5/public/static/", "web" => "/tp5/public/index.php/", ]; ``` #### 3. 新建模型處理數據并返回 上面的代碼我是把入庫驗證部分注釋掉了,直接進入后臺。 接下來就是MVC下面的M模型部分了, - 在index模塊下面新建一個model文件夾,再在model下面新建一個Admin.php文件(注意的是先有這個然后才有前面的代碼 $check=\\app\\index\\model\\Admin::login($name, $password);不然就會報錯),代碼: ``` <?php namespace app\index\model; use think\Input; class Admin extends \think\Model { /*登錄驗證*/ public static function login($name, $password) { $where['admin_name'] = $name; $where['admin_password'] = md5($password); $user=Admin::where($where)->find(); if ($user) { unset($user["password"]); session("ext_user", $user); return true; }else{ return false; } } } ``` 將上面的Login.php控制器里面的代碼更新如下(就可以真正進入后臺了): ``` <?php namespace app\index\controller; use think\Input; use think\Controller; use Captcha; use think\View; class Login extends Controller { public function login(){ return $this->fetch(); } public function logining() { $name = input('request.name'); $password = input('request.password'); $data = input('request.captcha'); // trace("########################################"); // trace($name); // trace($password); // trace($data); // trace("########################################"); if(!captcha_check($data)){ //驗證失敗 return $this->error("驗證碼錯誤","Login/login"); }; $check=\app\index\model\Admin::login($name, $password); if ($check) { header(strtolower("location:". config("web") . "index/admin/index")); exit(); }else{ return $this->error("用戶名或密碼錯誤","Login/login"); } } } ``` 上面的那個trace注釋部分可以自行刪除,主要是想提一下這種調試: 相比dump,這種調試更直觀,直接在runtime/log日志文件里面就可以看到調試的結果。加那么多#號是為了在日志文件中更明顯。特別是在一個彈出框里面輸入內容的時候,你用dump根本看不到輸入的內容,但是用trace接收輸入值,就能在日志中看到。 下面是我在登錄界面隨便輸入的用戶名驗證碼跟密碼三個變量,由于錯誤的話會立馬跳轉,用dump看不到剛剛輸入了什么,trace也能看到。 ``` [ info ] [ LOG ] INIT File [ log ] ######################################## [ log ] 121212 [ log ] 122121212 [ log ] 12121212 [ log ] ######################################## ``` 還有一個退出登錄,一個session管理,一個修改密碼,需要做,今晚更新上來,包括后臺的頁面樣式。做好過后,將登陸部分全部開放,源碼托管到github。 #### 4. 退出登錄。 - 在controller/Admin.php中新增代碼: ``` /*退出登錄*/ public function logout(){ \app\index\model\Admin::logout(); if (!session('?ext_user')) { header(strtolower("location: /login")); exit(); } return NULL; } ``` - \\app\\index\\model\\Admin::logout();看到這段代碼,很自然的就應該去\\app\\index\\model\\Admin.php里面加一個logout方法來清空session記錄, ``` /*退出登錄*/ public static function logout(){ session("ext_user", NULL); return ; } ``` 到這里,只是清空了session,阻止未登錄就訪問到后臺,你還要修改一下admin方法如下: ``` public function admin() { if (!session('?ext_user')) { header(strtolower("location: /login")); exit(); } return $this->fetch(); } ``` 這樣你再訪問http://thinkphp.com/admin 就會自動跳轉到登錄頁面,由于配置文件config.php里面設置了session: ``` 'session' => [ 'auto_start' => true, 'name' => 'login@', 'expire' => 1800, /*時間長度*/ ], ``` (所以你不退出,等半個小時,也會自動退出的,時間可以自行設定,別忘了修改時間的時候也去admin.html頁面去修改一下,這兩個時間是相對應的,作用就是session到期,頁面自動刷新一次,退出登錄) #### 5.修改密碼 - 修改密碼頁面也不能在未登錄的情況下訪問,所以,改: ``` /*修改密碼頁面*/ public function changepsw() { if (!session('?ext_user')) { header(strtolower("location: /login")); exit(); } return $this->fetch(); } ``` - 由于修改密碼頁面本質是一個表單提交,提交的時候出發一個事件changepassword(); controller/Admin.php新增 ``` /*修改密碼*/ public function changepassword(){ $oldpassword = md5(input('request.oldpassword')); $newpassword = input('request.newpassword'); $newpassword1 = input('request.newpassword1'); $name=session('ext_user')['admin_name']; $changepsw=\app\index\model\Admin::search($name); // dump($changepsw['admin_password']); $password=$changepsw['admin_password']; if ($password==$oldpassword ) { if ($newpassword==$newpassword1) { $updatepassword=\app\index\model\Admin::updatepassword($name,$newpassword); if ($updatepassword) { session("ext_user", NULL); return $this->success('修改成功,請重新登錄', ''.config("web_root").'/index/login/login'); }else{ return $this->error("修改密碼失敗"); } }else{ return $this->error("兩次輸入密碼不一致"); } }else{ return $this->error("原密碼輸入錯誤"); } } ``` 這里是不是新增了一個方法,我又去配置里加一條路由。 ``` "changepassword"=>"index/admin/changepassword", //修改密碼 ``` 代碼是一行一行敲出來的,希望新碼萌的你也能細心看完,最好跟著練習。 - 看到上面的 \\app\\index\\model\\Admin::search($name); \\app\\index\\model\\Admin::updatepassword($name,$newpassword); 這里要在\\app\\index\\model\\Admin.php下面新增兩個方法:search跟updatepassword,注意,這里是模型里面: ``` /*查詢一條數據*/ public static function search($name){ $where['admin_name'] = $name; $user=Admin::where($where)->find(); return $user; } /*更改用戶密碼*/ public static function updatepassword($name,$newpassword){ $where['admin_name'] = $name; $user=Admin::where($where)->update(['admin_password' => md5($newpassword)]); if ($user) { return true; }else{ return false; } } ``` 到這里,基本的功能就算完成了!接下來就是無腦的后臺頁面的設計,我寫的靜態頁面,也可以看一下,css部分每一行都是用心敲出來的,
                  <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>

                              哎呀哎呀视频在线观看