完成课程中的迷你小框架案例

Original 2019-05-13 23:56:10 200
abstract:<?php /**  * Created by PhpStorm.  * User: 普通用户  * Date: 2019/5/13  * Time: 0:14  */ namespace app\admin\controller; sess

20190513234448.jpg

20190513234523.jpg

<?php
/**
 * Created by PhpStorm.
 * User: 普通用户
 * Date: 2019/5/13
 * Time: 0:14
 */

namespace app\admin\controller;
session_start();
use app\model\User;
use pig\core\Controller;

class Index extends Controller
{
    public function __construct()
    {

        parent::__construct();
    }
    public function index()
    {
        $rows = (new User())->select('user',['uid','name','phone','country','birthday','weight',
                                        'height','add_time']);

        return $this->view->render('admin::index/index',['title'=>'演员管理系统',
            'rows'=>$rows,
            'loginUrl'=>'/admin/index/login',
            'logoutUrl'=>'/admin/index/logout',
            'indexUrl'=>'/admin/index/index',
            'insUrl'=>'/admin/index/insert',
            'editUrl'=>'/admin/index/edit',
            'delUrl'=>'admin/index/delete',
            'findUrl'=>'admin/index/find'
            ]);
    }

    public function login()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $res=(new User)->get('admin',['email','password','name'],[
                'AND'=>[
                    'email'=>$_POST['email'],
                    'password'=>sha1($_POST['password']),
                ]
            ]);
            if (false===$res){
                echo "<script>alert('邮箱或密码不正确');location.href='/';</script>";
            }else{
                $_SESSION['name']=$res['name'];
                echo "<script>alert('登陆成功');location.href='/';</script>";
            }
        }
    }

    public function  logout()
    {
        session_destroy();
        echo "<script>alert('退出成功');location.href='/';</script>";
    }

    public function insert()
    {
        return $this->view->render('admin::index/insert',[
           'title' =>'添加成员',
           'url'=>'/admin/index/add'
        ]);
    }

    public function add()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            (new User)->insert('user',[
                'name'=>$_POST['name'],
                'phone'=>$_POST['phone'],
                'country'=>$_POST['country'],
                'birthday'=>$_POST['birthday'],
                'weight'=>$_POST['weight'],
                'height'=>$_POST['height'],
                'add_time'=>strtotime($_POST['add_time'])
            ]);
            echo "<script>alert('添加成功');location.href='/';</script>";
        }

    }

    public function edit($uid='')
    {
        $row=(new User())->get('user',[
            'uid','name','phone','country','birthday','weight','height','height','add_time'],['uid'=>$uid]);

        return $this->view->render('admin::index/edit',[
            'title'=>'修改用户',
            'url'=>'/admin/index/save',
            'row'=> $row
        ]);
    }

    public function save($uid='')
    {
        if($_SERVER['REQUEST_METHOD']=='POST'){
            (new User())->update('user',[
                'name'=>$_POST['name'],
                'phone'=>$_POST['phone'],
                'country'=>$_POST['country'],
                'birthday'=>$_POST['birthday'],
                'weight'=>$_POST['weight'],
                'height'=>$_POST['height'],
                'add_time'=>strtotime($_POST['add_time'])
            ],['uid'=>$uid]);
        }

        echo "<script>alert('更新成功');location.href='/';</script>";

    }

    public function delete($id)
    {


           $ret= (new User())->delete('user',['uid' => $id]);
           if($ret){
               echo "<script>alert('删除成功');location.href='/';</script>";
           }else{
               echo "<script>alert('删除失败');location.href='/';</script>";
           }
    }

    public function find()
    {
        $find=$_POST['find'];
        $find_val=$_POST['find_val'];
        $rows = (new User())->select('user',['uid','name','phone','country','birthday','weight',
            'height','add_time'],[$find => $find_val]);

        if(!count($rows))
        {
            echo "<script>alert('找不到您需要的信息');location.href='/';</script>";
        }
         return $this->view->render('admin::index/index',['title'=>'演员管理系统',
            'rows'=>$rows,
            'loginUrl'=>'/admin/index/login',
            'logoutUrl'=>'/admin/index/logout',
            'indexUrl'=>'/admin/index/index',
            'insUrl'=>'/admin/index/insert',
            'editUrl'=>'/admin/index/edit',
            'delUrl'=>'admin/index/delete',
            'findUrl'=>'admin/index/find'
        ]);
    }
}
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title><?=$this->e($title);?></title>
    <link rel="stylesheet" href="/static/stytle.css">
</head>
<body>
        <?php if(isset($_SESSION['name']) && $_SESSION['name'] =='admin'):?>
            <p>欢迎管理员: <?=$this->e($_SESSION['name'])?> &nbsp;| &nbsp;<a href="<?=$this->e($logoutUrl);?>">退出</a></p>
        <?php else:?>
        <form action="<?=$this->e($loginUrl);?>" method="post">
            用户名:<input type="email" name="email" placeholder="请输入邮箱">
            密码:<input type="password" name="password" placeholder="请输入密码">
            <input type="submit" value="登录">
        </form>
        <?php endif;?>
        <table >
             <caption><h2>人员信息登记表 </h2>
                 <?php if(isset($_SESSION['name']) && $_SESSION['name'] =='admin'):?>
                 <a href="<?=$this->e($insUrl);?>" style="background: #ccc;">添加用户</a>
                 <?php endif;?>
             </caption>
        <tr>
            <th>工号</th>
            <th>姓名</th>
            <th>联系电话</th>
            <th>国籍</th>
            <th>出生日期</th>
            <th>体重</th>
            <th>身高</th>
            <th>入职时间</th>
            <?php if(isset($_SESSION['name']) && $_SESSION['name'] =='admin'):?>
            <th>操作</th>
            <?php endif;?>
        </tr>
            <?php foreach ($rows as $row):?>
        <tr>
            <td><?=$this->e($row['uid']);?></td>
            <td><?=$this->e($row['name']);?></td>
            <td><?=$this->e($row['phone']);?></td>
            <td><?=$this->e($row['country']);?></td>
            <td><?=$this->e($row['birthday']);?></td>
            <td><?=$this->e($row['weight']);?></td>
            <td><?=$this->e($row['height']);?></td>
            <td><?=$this->e(date("Y-m-d",$row['add_time']));?></td>
            <?php if(isset($_SESSION['name']) && $_SESSION['name'] =='admin'):?>
            <td><a href="<?=$this->e($editUrl)?>/id/<?=$this->e($row['uid']);?>">修改</a>
                <a href="javascript:;" onclick="del(<?=$this->e($row['uid']);?>)" style="color: red;return false">删除</a></td>
            <?php endif;?>
        </tr>
            <?php endforeach;?>
        </table>
        <form action="<?=$this->e($findUrl)?>" style="height: 100px;" method="post">
           搜索: <input type="text" name="find_val" placeholder="请输入要搜索的条件" value="">
            <select name="find">
                <option value ="uid">工号</option>
                <option value ="name">姓名</option>
                <option value ="phone">联系电话</option>
                <option value ="country">国籍</option>
                <option value ="birthday">出生日期</option>
                <option value ="weight">体重</option>
                <option value ="height">身高</option>
                <option value ="add_time">入职日期</option>
            </select>
            <input type="submit"value="开始搜索">
        </form>
<script>
    function del(id) {
        if(confirm("是否删除 id="+id+"的记录")){
            location.href='/admin/index/delete/id/'+id;
        }
    }
</script>
</body>
</html>

经过本章节对框架的编写开发,对mvc模式有一定的理解,从框架基础类,参数解析分发到模块实例化,模版渲染等知识的学习。后继课外再继续重复练习争取更加巩固下一阶段的thinkphp 开发技能。

Correcting teacher:查无此人Correction time:2019-05-14 09:13:46
Teacher's summary:完成的不错。框架的作用非常大,特别是开发大型项目,能减少很多工作。继续加油。

Release Notes

Popular Entries