github路径:https://github.com/zhengchuzhou/easyPhpFramework

一、目录结构及用途

二、相关代码:

1、入口文件(index.php):

<?php
/**
* 入口文件
* 1、定义常量
* 2、加载函数库
* 3、启动框架
*/
define('ROOT', realpath('./'));
define('CORE', ROOT.'/core');
define('APP', ROOT.'/app');
define('MODULE', 'app'); include "vendor/autoload.php"; // 引入composer加载的类 define('DEBUG', true);
if (DEBUG) {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();
ini_set('display_errors', 'On');
} else
ini_set('display_errors', 'Off'); include_once CORE.'/common/function.php';
include_once CORE.'/core.php'; spl_autoload_register('\core\core::load'); \core\core::run();

2、公用函数库(function.php):

<?php
/**
* 公告函数库
*/ function p($var)
{
if (is_bool($var))
var_dump($var);
elseif (is_null($var))
var_dump(NULL);
else {
echo "<pre style='position:relative; z-inenx: 1000; padding: 10px; border-radius: 5px; background: #F5F5F5;
border: 1px solid #aaa; font-size: 14px; line-height: 18px; opacity: 0.9;'>" . print_r($var, true) . "</pre>";
}
}

3、核心文件(core.php):

<?php
/**
* 核心文件
*/
namespace core; use core\lib\log; class core
{
public $assign = [];
static public function run()
{
log::init();
$route = new \core\lib\route(); $controller = $route->controller;
$action = $route->action;
$file = APP.'/controller/'.$controller.'Controller.php';
$class = '\\'.MODULE.'\\controller\\'.$controller.'Controller';
if (is_file($file)) {
include_once $file;
$class = new $class();
$class->$action();
} else
throw new \Exception('找不到控制器:'.$controller);
} static public function load($class)
{
// 自动加载类库
$class = str_replace('\\', '/', $class);
$file = ROOT.'/'.$class.'.php';
if (is_file($file))
include_once $file;
else
return false;
} public function assign($name, $value)
{
$this->assign[$name] = $value;
} public function display($file)
{
$file = APP.'/view/'.$file.'.php';
if (is_file($file)) {
extract($this->assign);
include $file;
} else
throw new \Exception('找不到该视图文件');
}
}

4、路由文件(route.php):

<?php
/**
* 路由文件
*/
namespace core\lib;
use \core\lib\conf; class route
{
public $controller;
public $action;
public function __construct()
{
$uri = $_SERVER['REQUEST_URI'];
if (isset($uri) && $uri != '/') {
$uriArr = explode('/', trim($uri, '/'));
if (isset($uriArr[0])) {
$this->controller = $uriArr[0];
unset($uriArr[0]);
}
if (isset($uriArr[1])) {
$this->action = $uriArr[1];
unset($uriArr[1]);
}
else
$this->action = conf::getConf('ACTION', 'route');
// uri多余部分转化成GET
for ($i = 2; $i < count($uriArr) + 2; $i += 2)
isset($uriArr[$i + 1]) ? $_GET[$uriArr[$i]] = $uriArr[$i + 1] : '';
} else {
$this->controller = conf::getConf('CONTROLLER', 'route');
$this->action = conf::getConf('ACTION', 'route');
}
log::log('controller:'.$this->controller.', action:'.$this->action.', get:'. json_encode($_GET));
}
}

5、数据库模型类(model.php):

<?php
/**
* model类
*/
namespace core\lib;
use \core\lib\conf; class model extends \PDO
{
public function __construct()
{
try {
$conf = conf::getAll('db');
parent::__construct($conf['DSN'], $conf['USERNAME'], $conf['PASSWD']);
} catch (\PDOException $e) {
p($e->getMessage());
}
}
}

6、配置文件类(conf.php):

<?php
/**
* 配置类
*/
namespace core\lib; class conf
{
static public $conf = [];
static public function getConf($name, $file)
{
/**
* 1、判断文件是否存在
* 2、判断配置是否存在
* 3、缓存配置
*/
$conf = self::getAll($file);
if (isset($conf[$name]))
return $conf[$name];
else
throw new \Exception('没有这个配置项'.$name);
} static public function getAll($file)
{
/**
* 1、判断文件是否存在
* 2、缓存配置
*/
if (!isset(self::$conf[$file])) {
$path = ROOT.'/core/config/'.$file.'.php';
if (!is_file($path)) {
throw new \Exception('找不到配置文件:'.$file);
die();
} else {
self::$conf[$file] = include $path;
}
}
return self::$conf[$file];
}
}

7、日志类(log.php):

<?php
/**
* 日志类
*/
namespace core\lib;
use \core\lib\conf; class log
{
static public $class;
/**
* 1、确定日志存储方式
* 2、写日志
*/
static public function init()
{
// 确定存储方式
$drive = conf::getConf('DRIVE', 'log');
$class = '\core\lib\log\\'.$drive;
self::$class = new $class;
} static public function log($message, $file = 'log')
{
self::$class->log($message, $file);
}
}

8、文件日志驱动类(file.php):

<?php
namespace core\lib\log; use core\lib\conf; class file
{
public $path;
public function __construct()
{
$this->path = conf::getConf('OPTION', 'log')['PATH'].date('Ymd').'/';
} public function log($message, $file = 'log')
{
/**
* 1、确定日志存储目录是否存在
* 不存在,新建目录
* 2、写入日志
*/
if (!is_dir($this->path))
mkdir($this->path, '0777', 'true');
$message = date('Y-m-d H:i:s') . ' ' . json_encode($message) . PHP_EOL;
file_put_contents($this->path.$file.'.php', $message, FILE_APPEND);
}
}

9、相关的配置文件:

数据库配置文件(db.php):

<?php
/**
* 数据库配置文件
*/
return array(
'DSN' => 'mysql:host=localhost; dbname=test',
'USERNAME' => 'root',
'PASSWD' => 'xy123456'
);

日志配置文件(log.php):

<?php
/**
* 日志配置文件
*/
return array(
'DRIVE' => 'file',
'OPTION' => [
'PATH' => ROOT.'/log/'
]
);

路由配置文件(route.php):

<?php
return array(
'CONTROLLER' => 'index',
'ACTION' => 'index'
);

三、nginx配置,优化url路径

if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
}

四、配置composer,加载php扩展

{
"name": "TEST PHP",
"description": "PHP Framework",
"type": "Framework",
"keywords": [
"PHP", "PHP Framework"
],
"require": {
"php": ">= 5.3.0",
"filp/whoops": "*",
"symfony/var-dumper": "*",
"catfan/medoo": "*"
}
}

开发基本的php框架的更多相关文章

  1. ASP.NET MVC5 网站开发实践(一) - 项目框架

    前几天算是开题了,关于怎么做自己想了很多,但毕竟没做过项目既不知道这些想法有无必要,也不知道能不能实现,不过邓爷爷说过"摸着石头过河"吧.这段时间看了一些博主的文章收获很大,特别是 ...

  2. plain framework 1(简约框架)一款主要用于网络(游戏)开发的C/C++框架 即将开源发布

    在我们的日常开发中,我们往往会遇到这种情况,当我们换了一个开发环境时很可能会重新利用一套新的框架进行开发.由于不同框架有着不同的接口,所以我们不得不花时间再次熟悉这些接口,这将造成开发时间上的重复,而 ...

  3. Asp.net Mvc模块化开发之分区扩展框架

    对于一个企业级项目开发,模块化是非常重要的. 默认Mvc框架的AreaRegistration对模块化开发真的支持很好吗?真的有很多复杂系统在使用默认的分区开发的吗?我相信大部分asp.net的技术团 ...

  4. ASP.NET MVC5 网站开发实践(一) - 项目框架(转)

    前几天算是开题了,关于怎么做自己想了很多,但毕竟没做过项目既不知道这些想法有无必要,也不知道能不能实现,不过邓爷爷说过“摸着石头过河”吧.这段时间看了一些博主的文章收获很大,特别是@kencery,依 ...

  5. Windows上python开发--2安装django框架

    Windows上python开发--2安装django框架 分类: 服务器后台开发2014-05-17 21:22 2310人阅读 评论(2) 收藏 举报 python django 上一篇文章中讲了 ...

  6. 10个用于Web开发的最好 Python 框架

    Python 是一门动态.面向对象语言.其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性.除了语言本身的设计目的之外,Python标准 库也是值得大家称赞的,Python甚至还 ...

  7. iOS开发之常用第三方框架(下载地址,使用方法,总结)

    iOS开发之常用第三方框架(下载地址,使用方法,总结) 说句实话,自学了这么久iOS,如果说我不知道的但是又基本上都摸遍了iOS相关知识,但是每次做项目的时候,遇到难一点的地方或者没试过的东西就闷了. ...

  8. Android开发中用到的框架、库介绍

    Android开发中用到的框架介绍,主要记录一些比较生僻的不常用的框架,不断更新中...... 网路资源:http://www.kuqin.com/shuoit/20140907/341967.htm ...

  9. 开发自己PHP MVC框架(一)

    本教程翻译自John Squibb 的Build a PHP MVC Framework in an Hour,但有所改动,原文地址:http://johnsquibb.com/tutorials 这 ...

  10. Phaser是一款专门用于桌面及移动HTML5 2D游戏开发的开源免费框架

    Phaser是一款专门用于桌面及移动HTML5 2D游戏开发的开源免费框架,提供JavaScript和TypeScript双重支持,内置游戏对象的物理属性,采用Pixi.js引擎以加快Canvas和W ...

随机推荐

  1. bzoj 1923: [Sdoi2010]外星千足虫【高斯消元】

    裸的异或高斯消元 #include<iostream> #include<cstdio> using namespace std; const int N=2005; int ...

  2. bzoj 1632: [Usaco2007 Feb]Lilypad Pond【bfs】

    直接bfs,在过程中更新方案数即可 #include<iostream> #include<cstdio> #include<queue> using namesp ...

  3. 码云 fatal: Authentication failed for

    最近push代码到码云时,push失败,提示fatal: Authentication failed for,解决方法就是: 在git命令行中输入 git config --system --unse ...

  4. poj 2349 Arctic Network(最小生成树的第k大边证明)

    题目链接: http://poj.org/problem?id=2349 题目大意: 有n个警戒部队,现在要把这n个警戒部队编入一个通信网络, 有两种方式链接警戒部队:1,用卫星信道可以链接无穷远的部 ...

  5. Nuget 自定义配置(官网)

    <?xml version="1.0" encoding="utf-8"?> <configuration> <config> ...

  6. vijos P1426兴奋剂检查 多维费用背包问题的hash

    https://vijos.org/p/1426 这是个好题,容易想到用dp[i][v1][v2][v3][v4][v5]表示在前i个物品中,各种东西的容量是那个的时候,能产生的最大价值. 时间不会T ...

  7. 外文翻译 《How we decide》赛场上的四分卫 第三节

    本书导言翻译 本章第二节 1982年,一位名叫Elliot的病人走进了神经科学家Antonio Damasio的办公室.几个月之前,一个小的肿瘤在它的大脑中被切除,切除点与大脑额叶非常靠近.在手术之前 ...

  8. AJPFX关于网络编程的理解

    1:网络编程(理解)        (1)网络编程:用Java语言实现计算机间数据的信息传递和资源共享        (2)网络编程模型        (3)网络编程的三要素              ...

  9. 2017广东工业大学程序设计竞赛决赛 G 等凹数字

    题意: Description 定义一种数字称为等凹数字,即从高位到地位,每一位的数字先非递增再非递减,不能全部数字一样,且该数是一个回文数,即从左读到右与从右读到左是一样的,仅形成一个等凹峰,如54 ...

  10. 【Python】第一个爬虫

    import urllib.request import re class DownPic: def __init__(self,url,re_str): self.url = url self.re ...