ThinkPHP5.1框架学习---不同角色跳转登录界面实现

将一个用原生php语言写好的选课系统用tp5搭建,根据学生/老师不同身份实现不同的功能。主要是想学习后端的技能,所以前端页面很简陋。
因为初学,所以需要花一些时间摸索。
之后准备自己学习完整搭建一个后台界面,可参见博客ThinkPHP5.1后台搭建项目1—项目介绍及登录界面的实现


(一)、登录界面实现

  • 目标:实现后台登录,并且根据不同角色跳转到不同界面
  • 语法:php+tp5.1+html+mysql
  • 说明:跳转后的界面没有写,只是实现了跳转。
1、tp5、数据库事先准备
  • 数据库名为xkxt,表student(含StuNo、ClassNo、StuName、Pwd),表teacher(含TeaNo、TeaName、Pwd、DepartNo)
  • 在config—database.php中配置数据库的名字、用户名、密码等等
  • 在原始tp5框架下新增application—admin—controller、view、model文件夹
  • controller使用多级目录stu/ShowCourse.php、tea/ShowCourse.php
2、登录界面实现(缺少的文件夹和文件自己新建)

public—admin.php
新建入口界面,直接复制index.php的内容即可(暂时还未实现前后端分离)
application—admin—view—Login—login.html

主要是设计前端界面,用于在控制器中渲染

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>登陆</title>
    <link rel="stylesheet" href="style.CSS">
</head>
<body>
<center>

    <table border="0" cellspacing="1" width="90%">
        <tr>
            <td> <form method="post" action=""><!--此处action为空,原本为跳转的xxx.php-->
                <table width="45%" border="1" cellspacing="0" cellpadding="1" align="center"
                       bordercolordark="#ecf5ff" bordercolorlight="#6699cc">
                    <tr>
                        <td><table width="100%" border="0" cellspacing="1" cellpadding="1">
                            <tr>
                                <td width="33%" align="right" height="30">用户名:</td>
                                <td width="67%"><input name="username" maxlength="20"  size="20"> </td>
                            </tr>
                            <tr>
                                <td width="33%" align="right" height="30">&nbsp; 码:</td>
                                <td width="67%"><input type="password" name="userpwd" maxlength="16"
                                                       size="20"> </td>
                            </tr>
                            <tr>
                                <td width="33%" align="right" height="30">&nbsp; 份:</td>
                                <td width="67%"> <select name="role">
                                    <option value="student">学生
                                    <option value="teacher">教师
                                </select> </td>
                            </tr>
                            <tr>
                                <td colspan="2" height="15"></td>
                            </tr>
                        </table>
                        </td>
                    </tr>
                    <tr align="center">
                        <td height="40">
                            <input type="submit" name="Submit" value="确定">
                            &nbsp;
                            <input type="reset" name="Submit2" value="重写">
                        </td>
                    </tr>
                </table>
            </form>
                 </td>
        </tr>
    </table>
</center>
</body>
</html>

application—admin—model—Admin.php
与数据库进行交互

<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class Admin extends Model
{
    public function login($data)
    {
        $username = $data['username'];
        $password = $data['userpwd'];
        $role = $data['role'];

        //根据角色对应不同查询操作
        if($role == 'student')
        {
            $result = Db::table('student')->where('StuNo',$username)->find();
        } else {
            $result = Db::table('teacher')->where('TeaNo',$username)->find();
        }

        //不同登录结果
        if($result){
            //(1)登陆成功
            if($result['Pwd'] == $password){
                session('username',$username);
                session('role',$role);
                return ($role=='student')?4:3;
            } else {
                //(2)密码错误
                return 2;
            }
        } else {
            //(3)用户不存在
            return 1;
        }
    }
}

application—admin----controller—Login.php
控制器获取数据交互结果并渲染页面

<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Admin;
class Login extends Controller
{
    public function index()
    {
        if(request()->isPost()){
            $admin = new Admin(); //调用model中的方法
            $data = input('post.');
            $status = $admin->login($data);
            if($status == 4){
                //注意多级目录的跳转路径,控制器下的文件夹名字.php名字/方法名
                $this->success('登陆成功,正在跳转!','stu.ShowCourse/index');
            } else if($status == 3){
                $this->success('登陆成功,正在跳转!','tea.ShowCourse/index');
            } else if($status == 2){
                $this->error('密码错误,请重新登录!');
            } else {
                $this->error('用户不存在!');
            }
        }
        return $this->fetch('Login/login'); //渲染页面
    }
}
3、跳转页面(只有框架)
  • 学生
    application—controller—stu—ShowCourse.php
<?php
namespace app\admin\controller\stu;
use think\Controller;
class ShowCourse extends Controller
{
    public function index()
    {
        return $this->fetch('stu/showcourse');//对应view---stu---showcourse.html
    }
}

  • 教师
    application—controller—tea—ShowCourse.php
<?php
namespace app\admin\controller\tea;
use think\Controller;
class ShowCourse extends Controller
{
    public function index()
    {
        return $this->fetch('tea/showcourse');//对应view---tea---showcourse.html
    }
}
4、界面的测试
浏览器访问: http://localhost/项目名字/public/admin.php/admin/login/index 即可打开登录界面

记得提前配置好tp5,保证localhost/tp5/public可以打开欢迎页面,这个可以自行百度。

总结一下个人对MVC框架的理解
Controller调用Model部分定义的与数据库交互相关的方法获得返回结果,View部分负责页面的显示。


  • 3
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值