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

Original 2019-10-22 22:32:31 1789
abstract://入口文件index.php

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

//入口文件index.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

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

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

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

namespace cfg\exd;

use League\Plates\Engine;

class view extends Engine{

protected $data=[];

public function __construct(){

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

}

}

// 扩展模型类model.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

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

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 '

'.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 "";

}else{

echo "";

}

}

//管理员退出

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 "" ;

}

}

}

//编辑用户

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 "" ;

}

}

}

//删除用户

public function doDel($id = null){

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

if($result){

echo "";

}

}

}

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

function isUser(){

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

return true;

}else{

return false;

}

}

//渲染的首页result.php

date_default_timezone_set('Asia/Shanghai');

require 'comm.php';

?>

<?=$title?>

欢迎管理员登录

请输入管理员 :

请输入密码 :


echo $title;

if(isUser()){

echo "{$_SESSION['username']}|退出";

}else{

echo '未登录';

}

?>

foreach($rows as $val) :

?>

Correcting teacher:天蓬老师Correction time:2019-10-23 12:11:05
Teacher's summary:代码写得很规范, 对于html与php混编, 使用了模板语法 , 不错的

Popular Entries

序号 ID 用户名 EMAIL 密码 创建时间 描述 操作