1.解压"tp5"压缩包到"thinkphp_5.0.24_with_extend\"(E);
2.把解压好的"tp5文件夹"—>改名"demo(可以起其它的名字)"->把demo文件夹拷贝到WWW目录;
3.在浏览器中输入"http://127.0.0.1/demo/public"—>查看tp5是否可以使用;
4.创建或导入一个数据库(我是导入的);
5.在application文件夹中—>创建admin文件夹—>在admin文件夹中—>分别创建controller、
model、view文件夹—>在controller文件夹中—>创建Login.php;
D:\phpStudy\WWW\demo\application\admin\controller\Login.php
内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Login as Log;
class Login extends Controller
{
public function index()
{
if (request()->isPost()){
$login = new Log;
$status = $login ->login(input( 'username' ),input( 'password' ));
if ( $status ==1){
return $this ->success( '登录成功,正在跳转!' , 'Index/index' );
} elseif ( $status ==2){
return $this ->error( '账号或者密码错误!' );
} else {
return $this ->error( '用户不存在!' );
}
}
return $this ->fetch( 'login' );
}
public function logout(){
session(null);
return $this ->success( '退出成功!' ,url( 'index' ));
}
}
|
6.在model文件夹中—>创建Login.php文件
D:\phpStudy\WWW\demo\application\admin\model\Login.php
内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php
namespace app\admin\model;
use think\Model;
class Login extends Model
{
public function login( $username , $password ){
$admin = \think\Db::name( 'admin' )->where( 'username' , '=' , $username )->find();
if ( $admin ){
if ( $admin [ 'password' ]==md5( $password )){
\think\Session::set( 'id' , $admin [ 'id' ]);
\think\Session::set( 'username' , $admin [ 'username' ]);
return 1;
} else {
return 2;
}
} else {
return 3;
}
}
}
|
7.在view文件夹中—>创建Login文件夹—>在Login文件夹中—>创建login.html文件
D:\phpStudy\WWW\demo\application\admin\view\Login\login.html
内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <!doctype html>
< html >
< head >
< meta charset = "UTF-8" >
< title >后台登录</ title >
< link href = "__PUBLIC__/static/admin/css/admin_login.css" rel = "stylesheet" type = "text/css" />
</ head >
< body >
< div class = "admin_login_wrap" >
< h1 >后台管理</ h1 >
< div class = "adming_login_border" >
< div class = "admin_input" >
< form action = "" method = "post" >
< ul class = "admin_items" >
< li >
< label for = "user" >用户名:</ label >
< input type = "text" name = "username" value = "admin" id = "user" size = "35" class = "admin_input_style" />
</ li >
< li >
< label for = "pwd" >密码:</ label >
< input type = "password" name = "password" value = "admin" id = "pwd" size = "35" class = "admin_input_style" />
</ li >
< li >
< input type = "submit" tabindex = "3" value = "提交" class = "btn btn-primary" />
</ li >
</ ul >
</ form >
</ div >
</ div >
</ div >
</ body >
</ html >
|
8.D:\phpStudy\WWW\demo\application\config.php
修改成:
就能看到Bug了!
模板文件不存在:D:\phpStudy\WWW\demo\public/../application/admin\view\login\login.html
view下的login文件名不对!!!*在controller和model下Login.php要大写Login,
在view下login.html要小写login!
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)出现Bug是没有链接数据库的
D:\phpStudy\WWW\demo\application\database.php
填写内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | return [
'type' => 'mysql' ,
'hostname' => '127.0.0.1' ,
'database' => 'youme' ,
'username' => 'root' ,
'password' => '****' ,
'hostport' => '' ,
'dsn' => '' ,
'params' => [],
'charset' => 'utf8' ,
'prefix' => 'ym_' ,
*****************************************************************************************
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'youhe.admin' doesn't exist(Bug)
|
D:\phpStudy\WWW\demo\application\admin\model\Login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <? php
namespace app\admin\model;
use think\Model;
class Login extends Model
{
public function login($username,$password){
// $admin= \think\Db::name('admin')->where('username','=',$username)->find();
$user= \think\Db::name('user')->where('username','=',$username)->find();
// if($admin){
if($user){
// if($admin['password']==md5($password)){
if($user['password']==$password){
// \think\Session::set('id',$admin['id']);
\think\Session::set('id',$user['id']);
// \think\Session::set('username',$admin['username']);
\think\Session::set('username',$user['username']);
return 1;
}else{
return 2;
}
}else{
return 3;
}
}
}
|
推荐教程:《TP5》
以上就是thinkphp5实现后台登录界面的方法的详细内容