返回小型用户管理系......登陆

小型用户管理系统迷你框架

肖凌2019-10-22 22:32:311796

163(OVVQ0U{))EE59O1`I@S.png

//入口文件index.php

<?php

         require 'vendor/autoload.php';

        define('ROOT_PATH',__DIR__);

        $config = require 'cfg/config.php';

        $qStr = $_SERVER['QUERY_STRING'];

        require 'cfg/base.php';

        use cfg\base;

        (new base($config,$qStr))->run();

//路由基类文件route.php

<?php

namespace cfg;

class route{

         protected $route=[];

         protected $pathInfo=[];

         protected $paras=[];

        public function __construct($route = []){

         $this->route = $route;

         }

        

         public function parse($qStr=''){

         $urlArr = explode('/',trim($qStr,'/'));

        $urlArr = array_filter($urlArr);

        switch(count($urlArr)){

                 case 0 :

                          $this->pathInfo = $this->route;

                          break;

                 case 1 :

                         $this->pathInfo['module'] = $urlArr[0];

                         break;

                 case 2 :

                         $this->pathInfo['module'] = $urlArr[0];

                         $this->pathInfo['controller'] = $urlArr[1];

                         break;

                 case 3 :

                         $this->pathInfo['module'] = $urlArr[0];

                         $this->pathInfo['controller'] = $urlArr[1];

                         $this->pathInfo['action'] = $urlArr[2];

                      break;

         default :

                     $this->pathInfo['module'] = $urlArr[0];

                     $this->pathInfo['controller'] = $urlArr[1];

                     $this->pathInfo['action'] = $urlArr[2];

                     $urlArr=array_slice($urlArr,3);

                           for($i=0; $i<count($urlArr); $i+=2){

                                 if(isset($urlArr[$i+1])){

                                     $this->paras[$urlArr[$i]] = $urlArr[$i+1];

                                 }

                         }

                     break;

                 }

             return $this;

         }

        public function dispatch(){

                    $module = $this->pathInfo['module'];

                    $controller ='app\\'.$module.'\\controller\\'.ucfirst($this->pathInfo['controller']);

                    $action = $this->pathInfo['action'];

                    if(!method_exists($controller,$action)){

                              header('location:/');

                     }

                    echo call_user_func_array([new $controller,$action],$this->paras);

         }

}

//基类文件base.php

<?php

         namespace cfg;

        class base{

                protected $app;

                protected $config;

                protected $qStr;

                public function __construct($config=[],$qStr=''){

                         $this->config = $config;

                         $this->qStr = $qStr;

                  }

                public function loader($class){

                        $urlPath=ROOT_PATH.'\\'.$class.'.php';

                         if(!file_exists($urlPath)){

                         header('location:/');

                        }

                    require $urlPath;

                    }

             public function run(){

                        spl_autoload_register([$this,'loader']);

                        (new route($this->config['route']))->parse($this->qStr)->dispatch();

                  }

            }

//扩展视图类view.php

        <?php

                 namespace cfg\exd;

                use League\Plates\Engine;

                class view extends Engine{

                            protected $data=[];

                            public function __construct(){

                              parent::__construct($directory = null, $fileExtension = 'php');

                         }

         }

// 扩展模型类model.php

<?php

         namespace cfg\exd;

        use Medoo\Medoo;

        class model extends Medoo{

                 public function __construct($db=''){

                        $database = require 'cfg/config.php';

                        parent::__construct($database['db']);

                 }

         }

//扩展控制器类controller.php

<?php

namespace cfg\exd;


class controller{


            protected $view;

            protected $data=[];

            public function __construct(){

                        $this->view = new view();

                        $this->setDir();

            }

            public function setDir(){

                        $this->view->setDirectory(ROOT_PATH.'/app/admin/view');

                        $this->view->addFolder('admin',ROOT_PATH.'/app/admin/view');

            }

            public function assign($name,$value){

                        $this->data[$name] = $value;

            }

         public function fetch($file){

                extract($this->data);

                require $file;

         }

        public function render($path,$data){

                        $this->data = $data;

                        return $this->view->render($path,$this->data);

             }

}

//模型控制器首页index.php

<?php

         namespace app\admin\controller;

        session_start();

        use cfg\exd\controller;

        use app\model\User;

        class Index extends controller {

        //渲染用户数据记录

         public function result(){

                $this->data['rows'] = (new User)->select('m_users',['user_id','username','email','password','detail']);

                 $this->data['title'] = '用户管理系统';

                 $this->data['page'] = [

                             'index' => 'admin/index/result',

                             'login' => 'admin/index/login',

                             'logout' => 'admin/index/logout',

                             'insert' => 'admin/index/insert',

                             'edit' =>'admin/index/edit',

                             'delete' => 'admin/index/delete',

                   ];

                     $this->dataArr =$this->data;  

                      //echo '<pre>'.var_export($this->dataArr,true);

                     return $this->render('index/result',$this->data);

         }

         //判断管理员登录

         public function login(){

         $username = $_POST['username'];

         $password = $_POST['password'];

                 if($res = (new User)->get('m_users',['username','password'],['AND'=>['username' => $username,'password' => $password]])){

                 $_SESSION['username'] = $username;

                 echo "<script>alert('登录成功');location.href='index.php?admin/index/result'</script>";

                 }else{

                         echo "<script>alert('登录失败');location.href='index.php?admin/index/result'</script>";

                 }

         }

         //管理员退出

         public function logout(){

                 session_destroy();

                 header('location:index.php?admin/index/result');

         }

         //添加数据

         public function insert(){

                 $this->data['title']='用户管理系统 - 添加用户';

                 return $this->render('index/insert',$this->data);

         }

         //添加数据到数据库

         public function doInsert(){

                 if($_SERVER['REQUEST_METHOD']=='POST'){

                 $username = $_POST['username'];

                 $email = $_POST['email'];

                 $password = $_POST['password'];

                 $detail = $_POST['detail'];

                 $stat = (new User)->insert('m_users',['username'=>$username,'email'=>$email,'password'=>$password,'detail'=>$detail]);

                  if($stat){

                          echo "<script>alert('添加成功');location.href='index.php?admin/index/result'</script>" ;

                     }

              }

         }

         //编辑用户

         public function edit($id){

                      $this->data['title']='用户管理系统 - 编辑用户';

                        $this->data['rows'] = (new User)->select('m_users',['user_id','username','email','password','detail'],['user_id'=>$id]);

                         return $this->render('index/edit',$this->data);

         }

         //处理编辑用户到数据库

         public function doEdit(){

                 if($_SERVER['REQUEST_METHOD']=='POST'){

                         $id = $_POST['id'];

                         $username = $_POST['username'];

                         $email = $_POST['email'];

                         $password = $_POST['password'];

                         $detail = $_POST['detail'];

                          $stat = (new User)->update('m_users',['username'=>$username,'email'=>$email,'password'=>$password,'detail'=>$detail],['user_id'=>$id]);

        

                         if($stat){

                                  echo "<script>alert('更新成功');location.href='index.php?admin/index/result'</script>" ;

                         }

                  }

         }

         //删除用户

         public function doDel($id = null){

                        $result = (new User)->delete('m_users',['user_id'=>$id]);

                 if($result){

                         echo "<script>alert('删除成功');location.href='index.php?admin/index/result';</script>";

                             }

                   }

            }

//判断用户是否登录公共文件comm.php

<?php

function isUser(){

         if(isset($_SESSION['username']) && $_SESSION['username'] == 'admin'){

                  return true;

         }else{

                 return false;

         }

}

//渲染的首页result.php

<?php 

         date_default_timezone_set('Asia/Shanghai');

         require 'comm.php';

 ?>

<!DOCTYPE html>

<html>

             <head>

                     <meta charset="utf-8">

                     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

                     <link rel="shortcut icon" type="image/x-icon" href="static/img/favicon.ico">

                     <link rel="stylesheet" type="text/css" href="static/layui/css/layui.css">

                     <link rel="stylesheet" type="text/css" href="static/font-awesome/css/font-awesome.min.css">

                     <link rel="stylesheet" type="text/css" href="static/css/style.css">

                     <script type="text/javascript" src="static/layui/layui.js"></script>

                     <script type="text/javascript" src="static/js/jq_3.3.1_mi.js"></script>

                     <title><?=$title?></title>

             </head>

         <body>

         <?php if(isUser()) :?>

                  <p>欢迎管理员<?=$_SESSION['username']?>登录</p>

         <?php else :?>

                      <form action="index.php?<?=$page['login']?>" method="post">

                                 请输入管理员 : <input type="text" name="username" required>

                                 请输入密码 :  <input type="password" name="password" required>

                      <button>登录</button>

                      </form>

         <?php endif?>

        <h2>

         <?php

                 echo $title;

                 if(isUser()){

                                      echo "<span style='color:red;margin-left: 20px;'>{$_SESSION['username']}</span>|<a href='index.php?{$page['logout']}'>退出</a>";

                  }else{

                                      echo '<span style="color:red;margin-left: 20px;">未登录</span>';

                 }

          ?></h2>

            <table class="layui-table">

                   <colgroup>

                             <col width="60">

                             <col width="50">

                             <col width="120">

                             <col width="150">

                   </colgroup>

           <thead>

                     <tr>

                               <th>序号</th>

                               <th>ID</th>

                               <th>用户名</th>

                               <th>EMAIL</th>

                               <th>密码</th>

                               <th>创建时间</th>       

                               <th>描述</th>

                               <?php if(isUser()) :?>

                               <th>操作</th>

                               <?php endif ?>

                     </tr> 

           </thead>

           <tbody>

                 <?php     $i=1;

                                foreach($rows as $val) :

                 ?>

             <tr>

                       <td><?=$i?></td>

                       <td class="userId"><?=$val['user_id']?></td>

                       <td><?=$val['username']?></td>

                       <td><?=$val['email']?></td>

                       <td><?=$val['password']?></td>

                       <td><?=date('Y-m-d h:i:s',time())?></td>

                       <td><?=$val['detail']?></td>

           <?php if(isUser()) :?>

               <td&a    

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送