摘要:框架目录app文件夹是应用目录pig文件夹是核心文件vendor是第三方类库.htaccess url重写文件index.php项目入口文件以下是关键文件代码index.php<?php session_start(); //加载composer自动加载器 require 'vendor/autoload.php'; //加载框架基础类 require&
框架目录

app文件夹是应用目录
pig文件夹是核心文件
vendor是第三方类库
.htaccess url重写文件
index.php项目入口文件
以下是关键文件代码
index.php
<?php
session_start();
//加载composer自动加载器
require 'vendor/autoload.php';
//加载框架基础类
require 'pig/Base.php';
//定义项目根目录
define('ROOT_PATH',__DIR__.'/');
$config = require 'pig/config.php';
(new \pig\Base($config,$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']))->run();基类控制器
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/1/10
* Time: 20:33
*/
namespace pig\core;
use pig\core\View;
class Controller
{
protected $view = null;
public function __construct()
{
$this->view = new \pig\core\View();
$this->config();
}
public function config()
{
//设置后台模板目录
$this->view->setDirectory(ROOT_PATH.'app/admin/view');
//给模板目录起个别名
$this->view->addFolder('admin',ROOT_PATH.'app/admin/view');
$this->view->setDirectory(ROOT_PATH.'app/home/view');
$this->view->addFolder('home',ROOT_PATH.'app/home/view');
}
}基类视图
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/1/9
* Time: 20:59
*/
namespace pig\core;
use League\Plates\Engine;
class View extends Engine
{
//变量容器
protected $data = [];
/**
* View constructor.
* @param null $directory 模板目录
* @param string $fileExtension 模板文件扩展
*/
public function __construct($directory = null, $fileExtension = 'php')
{
parent::__construct($directory, $fileExtension);
}
}基类模型
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/1/9
* Time: 20:51
*/
namespace pig\core;
use Medoo\Medoo;
class Model extends Medoo
{
public function __construct()
{
$config = require ROOT_PATH.'pig/config.php';
$options = $config['db'];
parent::__construct($options);
}
}配置文件
<?php //配置文件 return [ 'app'=>[ 'debug'=>true ], 'route'=>[ 'module'=>'admin', 'controller'=>'Index', 'action'=>'index' ], 'db'=>[ 'database_type' => 'mysql', 'database_name' => 'frame', 'server' => '127.0.0.1', 'username' => 'root', 'password' => 'root' ] ];
路由文件
<?php
namespace pig;
class Route
{
//保存路由配置信息
protected $route = [];
//保存url路径信息
protected $pathinfo = [];
//保存url参数
protected $param = [];
public function __construct($route)
{
$this->route = $route;
}
//解析路由
//http://frame.io/[index.php]/admin/Index/index
public function parse($url)
{
if(strpos($url,'.php')){
$queryStr = strchr($url,'.php');
$queryStr = trim(substr($queryStr,4),'/');
}else{
$domainLen = strlen($_SERVER['SERVER_NAME']);
$queryStr = strchr($url,$_SERVER['SERVER_NAME']);
$queryStr = trim(substr($queryStr,$domainLen),'/');
}
$queryArr = explode('/',$queryStr);
$queryArr = array_filter($queryArr);
switch (count($queryArr)){
case 0:
$this->pathinfo = $this->route;
break;
case 1:
$this->pathinfo['module'] = $queryArr[0];
break;
case 2:
$this->pathinfo['module'] = $queryArr[0];
$this->pathinfo['controller'] = $queryArr[1];
break;
case 3:
$this->pathinfo['module'] = $queryArr[0];
$this->pathinfo['controller'] = $queryArr[1];
$this->pathinfo['action'] = $queryArr[2];
break;
default :
$this->pathinfo['module'] = $queryArr[0];
$this->pathinfo['controller'] = $queryArr[1];
$this->pathinfo['action'] = $queryArr[2];
//处理参数
$paramArr = array_slice($queryArr,3);
for($i=0;$i<count($paramArr);$i+=2){
if(!empty($paramArr[$i+1])){
$this->param[$paramArr[$i]] = $paramArr[$i+1];
}
}
}
return $this;
}
//路由分发
public function dispatch()
{
$module = $this->getModule();
$controller = $this->getController();
$action = $this->getAction();
if(!method_exists($controller,$action)){
header('Location:/');
}
return call_user_func_array([new $controller,$action],$this->param);
}
public function getModule()
{
return $this->pathinfo['module'];
}
public function getParam()
{
return $this->param;
}
public function getController()
{
$module = $this->getModule();
if(empty($this->pathinfo['controller'])){
$this->pathinfo['controller'] = $this->route['controller'];
}
$controller = 'app\\'.$module.'\controller\\'.ucfirst($this->pathinfo['controller']);
return $controller;
}
public function getAction()
{
if(empty($this->pathinfo['action'])){
$this->pathinfo['action'] = $this->route['action'];
}
return $this->pathinfo['action'];
}
}
//$url = 'http://frame.io/admin/Index/index/name/peter/age/30';
//$config = require __DIR__.'/config.php';
//$route = new Route($config['route']);
//$route->parse($url);
//require __DIR__.'/../app/admin/controller/Index.php';
//$route->dispatch();框架引导文件
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/1/8
* Time: 20:59
*/
namespace pig;
class Base
{
//路由配置信息
protected $config = [];
//请求字符串
protected $queryStr = '';
public function __construct($config,$queryStr)
{
$this->config = $config;
$this->queryStr = $queryStr;
}
public function setDebug()
{
if($this->config['app']['debug']){
error_reporting(E_ALL);
ini_set('display_errors','On');
}else{
ini_set('display_errors','Off');
ini_set('log_errors','On');
}
}
public function loader($class)
{
$path = str_replace('\\','/',$class).'.php';
if(!file_exists($path)){
header('Location:/');
}
require $path;
}
public function run()
{
//调试模式
$this->setDebug();
//自动加载
spl_autoload_register([$this,'loader']);
//请求分发
(new Route($this->config['route']))->parse($this->queryStr)->dispatch();
}
}应用实现的主控制器文件
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/1/8
* Time: 15:54
*/
namespace app\admin\controller;
use app\model\User;
use pig\core\Controller;
class Index extends Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$model = new User();
$rows = $model->select($model->getTable(),['id','name','dept','art','email','create_time'],[
'dept[~]'=>isset($_POST['dept'])?$_POST['dept']:null
]);
echo $this->view->render('admin::index/index',[
'title'=>'武林高手排行榜',
'rows'=>$rows,
'searchUrl'=>'/admin/index/index',
'loginUrl'=>'/admin/index/login',
'logoutUrl'=>'/admin/index/logout',
'insertPage'=>'/admin/index/insert',
'editUrl'=>'/admin/index/edit',
'delUrl'=>'/admin/index/del'
]);
}
public function login()
{
$model = new User();
$res = $model->get('admin',['id','name'],[
"AND" => [
"password" => sha1($_POST['password']),
"email" => $_POST['email']
]
]);
if($res){
$_SESSION['name'] = $res['name'];
$_SESSION['id'] = $res['id'];
echo '<script>alert("登录成功");location.href="/";</script>';
}else{
echo '<script>alert("邮箱或密码错误");location.href="/";</script>';
}
}
public function logout()
{
session_destroy();
echo '<script>alert("退出成功");location.href="/";</script>';
}
public function insert()
{
echo $this->view->render('admin::index/insert',['doInsertUrl'=>'/admin/index/doInsert']);
}
public function doInsert()
{
$model = new User();
$data = $_POST;
$data['create_time'] = time();
$res = $model->insert($model->getTable(),$data);
if($res){
echo '<script>alert("插入成功");location.href="/";</script>';
}else{
echo '<script>alert("插入失败");</script>';
}
}
public function edit($id)
{
$model = new User();
$res = $model->get($model->getTable(),'*',['id'=>$id]);
echo $this->view->render('admin::index/edit',['res'=>$res,'doEditUrl'=>'/admin/index/doEdit']);
}
public function doEdit($id)
{
$model = new User();
$res = $model->update($model->getTable(),$_POST,['id'=>$id]);
if($res){
echo '<script>alert("编辑成功");location.href="/";</script>';
}else{
echo '<script>alert("编辑失败");</script>';
}
}
public function del($id)
{
$model = new User();
$res = $model->delete($model->getTable(),['id'=>$id]);
if($res){
echo '<script>alert("删除成功");location.href="/";</script>';
}else{
echo '<script>alert("删除失败");</script>';
}
}
}项目效果图

批改老师:天蓬老师批改时间:2019-01-14 10:06:44
老师总结:复制的时候,居然连注释掉的调试信息也一并复制过来, 难道就不能自己动手写写吗? 如果你总是这样的话, 学习进度和效率会很低的, 下次希望你能自己亲自动手写写, 画一下流程图, 把老师的这个框架案例的画一下, 理解其中包括的知识点