登錄-Thinkphp5.1開發後台管理系統
- 2019 年 12 月 27 日
- 筆記
登錄,是每一個後台管理系統的主要模組,請注意「登錄」而不是「登陸」。
框架採用路由模式,本文包含:開啟路由、路由配置、公共控制器、登錄控制器等內容;
一、開啟路由
將config目錄下的app.php文件(thinkphp5.1版本核心配置文件)內的路由配置項設置為開啟狀態;
// 是否強制使用路由 'url_route_must' => true,
二、路由配置
thinkphp5.1版本將路由文件單獨拿了出來,不同於5.1版本放置與application目錄下,新版本將其放置在public同級目錄下。目錄名為『route』,默認路由文件名『route.php』。
我們將默認文件內的程式碼一刪除,插入程式碼二,路由採用分組+請求方式形式,增強路由解析能力和請求安全問題。
程式碼一:
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <[email protected]> // +---------------------------------------------------------------------- Route::get('think', function () { return 'hello,ThinkPHP5!'; }); Route::get('hello/:name', 'index/hello'); return [ ];
程式碼二:
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <[email protected]> // +---------------------------------------------------------------------- //Administration Route::group('admin', function () { Route::rule('', 'admin/Index/index'); //後台首頁 Route::group('Login', function () { Route::get('index', 'admin/Login/index'); //登錄 Route::post('login', 'admin/Login/dologin'); //登錄處理 Route::get('code', 'admin/Login/docode'); //驗證碼 Route::get('out', 'admin/Login/doout'); //退出登錄 }); }); return [];
三、公共控制器
在application/admin/controller下新建名為『Base.php』的控制器文件,這裡說下新建方法,可以通過5.1版本的命令行模式進行新建,也可以手動新建,命令行模式會將增刪改查等方法自動建立,便於開發,適用於非資料庫操作控制器。我們將採用手動新建模式,因為控制器不含有資料庫操作,故不採用命令行模式。
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019217 0017 * Time: 10:45 */ namespace appadmincontroller; use thinkApp; use thinkController; class Base extends Controller { public function __construct(App $app = null) { parent::__construct($app); $userName = session('userName','','admin'); if($userName){ $this->assign('userName',$userName); }else{ $this->redirect('Login/index'); } } }
將controller目錄下的Index控制器(即Index.php文件)繼承Base控制器;
<?php namespace appadmincontroller; class Index extends Base { /** * 後台首頁 * @return thinkresponseView */ public function index() { return view('Index/index'); } }
在Login控制器內寫入登錄頁面、登錄請求、登錄驗證碼方法;
這裡需要注意Login控制器不需要繼承Base控制器;
一、登錄頁面
/** * 登錄 * @return thinkresponseView */ public function index() { return view('Login/login'); }
二、登錄請求
/** * 登錄處理 */ public function dologin() { $userName = input('post.username'); $userPass = input('post.userpass'); if(!$userName || !$userPass) { $this->error('請填寫帳號或密碼,100'); } $code = input('post.code'); if(!captcha_check($code)) { $this->error('驗證碼錯誤,101'); } $res = $this->user_model->getOne(['username' => $userName], 'userpass,display'); if(!$res) { $this->error('帳號或密碼錯誤,102'); } if($res['display'] == 2) { $this->error('您的賬戶已凍結,103'); } if(md5($userPass) !== $res['userpass']) { $this->error('帳號或密碼錯誤,104'); } session('userName', $userName, 'admin'); $this->success('登錄成功'); }
三、登錄驗證碼
/** * 驗證碼 * @return mixed */ public function docode() { $config = [ 'fontSize' => 30, 'length' => 3, 'useCurve' => false, 'useNoise' => false, 'codeSet' => '023456789' ]; $captcha = new Captcha($config); return $captcha->entry(); }
四、登錄頁面
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>登錄 - Shirley 後台大布局</title> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="format-detection" content="telephone=no"> <link rel="stylesheet" href="__Static__/css/layui.css" media="all"> <link rel="stylesheet" href="__Static__/css/login.css"> </head> <body class="layui-layout-body"> <div class="layui-row"> <div class="layui-col-xs1 layui-col-sm2 layui-col-md4 left"></div> <div class="layui-col-xs10 layui-col-sm8 layui-col-md4 center"> <div class="login_logo"> <h1 align="center">Shirley 後台管理模板系統</h1> </div> <form action="{:url('Login/login')}" method="post" class="alyui-form"> <div class="layui-form-item"> <div class="layui-inline"> <label class="layui-form-label">帳號</label> <div class="layui-input-inline"> <input type="text" name="username" lay-verify="required|text" class="layui-input"> </div> </div> <div class="layui-inline"> <label class="layui-form-label">密碼</label> <div class="layui-input-inline"> <input type="password" name="userpass" lay-verify="required|password" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-inline"> <label class="layui-form-label">驗證碼</label> <div class="layui-input-inline login-form-code"> <input type="text" name="code" lay-verify="required|text" autocomplete="off" class="layui-input"> </div> <div class="layui-input-inline login-form-code"> <img src="{:url('Login/code')}" alt="驗證碼" id="randcode" onclick="this.src=this.src+'?'"> </div> </div> <div class="login_btn"> <input type="submit" class="layui-btn layui-btn-fluid" value="登錄"> </div> </div> </form> <p id="copyright">© www.shenlin.ink</p> </div> <div class="layui-col-xs1 layui-col-sm2 layui-col-md4 right"></div> </div> <script src="__Static__/layui.js"></script> </body> </html>
五、thinkphp5.1版本驗證碼生成問題