<IfModule mod_rewrite.c>
RewriteEngine On
# 确保请求路径不是一个文件名或目录
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# 重定向所有请求到 index.php?url=PATHNAME
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<?php
// 应用目录为当前目录
define('APP_PATH', __DIR__.'/');
// 开启调试模式
define('APP_DEBUG', true);
// 网站根URL
define('APP_URL', 'http://localhost/fastphp');
// 加载框架
require './fastphp/FastPHP.php';
<?php
// 初始化常量
defined('FRAME_PATH') or define('FRAME_PATH', __DIR__.'/');
defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']).'/');
defined('APP_DEBUG') or define('APP_DEBUG', false);
defined('CONFIG_PATH') or define('CONFIG_PATH', APP_PATH.'config/');
defined('RUNTIME_PATH') or define('RUNTIME_PATH', APP_PATH.'runtime/');
// 包含配置文件
require APP_PATH . 'config/config.php';
//包含核心框架类
require FRAME_PATH . 'Core.php';
// 实例化核心类
$fast = new Core;
$fast->run();
<?php
/** 变量配置 **/
define('DB_NAME', 'todo');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');
<?php
/**
* FastPHP核心框架
*/
class Core
{
// 运行程序
function run()
{
spl_autoload_register(array($this, 'loadClass'));
$this->setReporting();
$this->removeMagicQuotes();
$this->unregisterGlobals();
$this->Route();
}
// 路由处理
function Route()
{
$controllerName = 'Index';
$action = 'index';
if (!empty($_GET['url'])) {
$url = $_GET['url'];
$urlArray = explode('/', $url);
// 获取控制器名
$controllerName = ucfirst($urlArray[0]);
// 获取动作名
array_shift($urlArray);
$action = empty($urlArray[0]) ? 'index' : $urlArray[0];
//获取URL参数
array_shift($urlArray);
$queryString = empty($urlArray) ? array() : $urlArray;
}
// 数据为空的处理
$queryString = empty($queryString) ? array() : $queryString;
// 实例化控制器
$controller = $controllerName . 'Controller';
$dispatch = new $controller($controllerName, $action);
// 如果控制器存和动作存在,这调用并传入URL参数
if ((int)method_exists($controller, $action)) {
call_user_func_array(array($dispatch, $action), $queryString);
} else {
exit($controller . "控制器不存在");
}
}
// 检测开发环境
function setReporting()
{
if (APP_DEBUG === true) {
error_reporting(E_ALL);
ini_set('display_errors','On');
} else {
error_reporting(E_ALL);
ini_set('display_errors','Off');
ini_set('log_errors', 'On');
ini_set('error_log', RUNTIME_PATH. 'logs/error.log');
}
}
// 删除敏感字符
function stripSlashesDeep($value)
{
$value = is_array($value) ? array_map('stripSlashesDeep', $value) : stripslashes($value);
return $value;
}
// 检测敏感字符并删除
function removeMagicQuotes()
{
if ( get_magic_quotes_gpc()) {
$_GET = stripSlashesDeep($_GET );
$_POST = stripSlashesDeep($_POST );
$_COOKIE = stripSlashesDeep($_COOKIE);
$_SESSION = stripSlashesDeep($_SESSION);
}
}
// 检测自定义全局变量(register globals)并移除
function unregisterGlobals()
{
if (ini_get('register_globals')) {
$array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
foreach ($array as $value) {
foreach ($GLOBALS[$value] as $key => $var) {
if ($var === $GLOBALS[$key]) {
unset($GLOBALS[$key]);
}
}
}
}
}
// 自动加载控制器和模型类
static function loadClass($class)
{
$frameworks = FRAME_PATH . $class . '.class.php';
$controllers = APP_PATH . 'application/controllers/' . $class . '.class.php';
$models = APP_PATH . 'application/models/' . $class . '.class.php';
if (file_exists($frameworks)) {
// 加载框架核心类
include $frameworks;
} elseif (file_exists($controllers)) {
// 加载应用控制器类
include $controllers;
} elseif (file_exists($models)) {
//加载应用模型类
include $models;
} else {
/* 错误代码 */
}
}
}
<?php
/**
* 控制器基类
*/
class Controller
{
protected $_controller;
protected $_action;
protected $_view;
// 构造函数,初始化属性,并实例化对应模型
function __construct($controller, $action)
{
$this->_controller = $controller;
$this->_action = $action;
$this->_view = new View($controller, $action);
}
// 分配变量
function assign($name, $value)
{
$this->_view->assign($name, $value);
}
// 渲染视图
function __destruct()
{
$this->_view->render();
}
}
<?php
class Model extends Sql
{
protected $_model;
protected $_table;
function __construct()
{
// 连接数据库
$this->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// 获取模型名称
$this->_model = get_class($this);
$this->_model = rtrim($this->_model, 'Model');
// 数据库表名与类名一致
$this->_table = strtolower($this->_model);
}
function __destruct()
{
}
}
<?php
class Sql
{
protected $_dbHandle;
protected $_result;
// 连接数据库
public function connect($host, $user, $pass, $dbname)
{
try {
$dsn = sprintf("mysql:host=%s;dbname=%s;charset=utf8", $host, $dbname);
$this->_dbHandle = new PDO($dsn, $user, $pass, array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
} catch (PDOException $e) {
exit('错误: ' . $e->getMessage());
}
}
// 查询所有
public function selectAll()
{
$sql = sprintf("select * from `%s`", $this->_table);
$sth = $this->_dbHandle->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
// 根据条件 (id) 查询
public function select($id)
{
$sql = sprintf("select * from `%s` where `id` = '%s'", $this->_table, $id);
$sth = $this->_dbHandle->prepare($sql);
$sth->execute();
return $sth->fetch();
}
// 根据条件 (id) 删除
public function delete($id)
{
$sql = sprintf("delete from `%s` where `id` = '%s'", $this->_table, $id);
$sth = $this->_dbHandle->prepare($sql);
$sth->execute();
return $sth->rowCount();
}
// 自定义SQL查询,返回影响的行数
public function query($sql)
{
$sth = $this->_dbHandle->prepare($sql);
$sth->execute();
return $sth->rowCount();
}
// 新增数据
public function add($data)
{
$sql = sprintf("insert into `%s` %s", $this->_table, $this->formatInsert($data));
return $this->query($sql);
}
// 修改数据
public function update($id, $data)
{
$sql = sprintf("update `%s` set %s where `id` = '%s'", $this->_table, $this->formatUpdate($data), $id);
return $this->query($sql);
}
// 将数组转换成插入格式的sql语句
private function formatInsert($data)
{
$fields = array();
$values = array();
foreach ($data as $key => $value) {
$fields[] = sprintf("`%s`", $key);
$values[] = sprintf("'%s'", $value);
}
$field = implode(',', $fields);
$value = implode(',', $values);
return sprintf("(%s) values (%s)", $field, $value);
}
// 将数组转换成更新格式的sql语句
private function formatUpdate($data)
{
$fields = array();
foreach ($data as $key => $value) {
$fields[] = sprintf("`%s` = '%s'", $key, $value);
}
return implode(',', $fields);
}
}
<?php
/**
* 视图基类
*/
class View
{
protected $variables = array();
protected $_controller;
protected $_action;
function __construct($controller, $action)
{
$this->_controller = $controller;
$this->_action = $action;
}
/** 分配变量 **/
function assign($name, $value)
{
$this->variables[$name] = $value;
}
/** 渲染显示 **/
function render()
{
extract($this->variables);
$defaultHeader = APP_PATH . 'application/views/header.php';
$defaultFooter = APP_PATH . 'application/views/footer.php';
$controllerHeader = APP_PATH . 'application/views/' . $this->_controller . '/header.php';
$controllerFooter = APP_PATH . 'application/views/' . $this->_controller . '/footer.php';
// 页头文件
if (file_exists($controllerHeader)) {
include ($controllerHeader);
} else {
include ($defaultHeader);
}
// 页内容文件
include (APP_PATH . 'application/views/' . $this->_controller . '/' . $this->_action . '.php');
// 页脚文件
if (file_exists($controllerFooter)) {
include ($controllerFooter);
} else {
include ($defaultFooter);
}
}
}
CREATE DATABASE `todo` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE `todo`; CREATE TABLE `item` ( `id` int(11) NOT NULL auto_increment, `item_name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; INSERT INTO `item` VALUES(1, 'Hello World.'); INSERT INTO `item` VALUES(2, 'Lets go!');
<?php
class ItemModel extends Model
{
/* 业务逻辑层实现 */
}
<?php
class ItemController extends Controller
{
// 首页方法,测试框架自定义DB查询
public function index()
{
$items = (new ItemModel)->selectAll();
$this->assign('title', '全部条目');
$this->assign('items', $items);
}
// 添加记录,测试框架DB记录创建(Create)
public function add()
{
$data['item_name'] = $_POST['value'];
$count = (new ItemModel)->add($data);
$this->assign('title', '添加成功');
$this->assign('count', $count);
}
// 查看记录,测试框架DB记录读取(Read)
public function view($id = null)
{
$item = (new ItemModel)->select($id);
$this->assign('title', '正在查看' . $item['item_name']);
$this->assign('item', $item);
}
// 更新记录,测试框架DB记录更新(Update)
public function update()
{
$data = array('id' => $_POST['id'], 'item_name' => $_POST['value']);
$count = (new ItemModel)->update($data['id'], $data);
$this->assign('title', '修改成功');
$this->assign('count', $count);
}
// 删除记录,测试框架DB记录删除(Delete)
public function delete($id = null)
{
$count = (new ItemModel)->delete($id);
$this->assign('title', '删除成功');
$this->assign('count', $count);
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title ?></title>
<style>
.item {
width:400px;
}
input {
color:#222222;
font-family:georgia,times;
font-size:24px;
font-weight:normal;
line-height:1.2em;
color:black;
}
a {
color:blue;
font-family:georgia,times;
font-size:20px;
font-weight:normal;
line-height:1.2em;
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
h1 {
color:#000000;
font-size:41px;
letter-spacing:-2px;
line-height:1em;
font-family:helvetica,arial,sans-serif;
border-bottom:1px dotted #cccccc;
}
h2 {
color:#000000;
font-size:34px;
letter-spacing:-2px;
line-height:1em;
font-family:helvetica,arial,sans-serif;
}
</style>
</head>
<body>
<h1><?php echo $title ?></h1>
footer.php,内容:
</body>
</html>
<form action="<?php echo APP_URL ?>/item/add" method="post">
<input type="text" value="点击添加" onclick="this.value=''" name="value">
<input type="submit" value="添加">
</form>
<br/><br/>
<?php $number = 0?>
<?php foreach ($items as $item): ?>
<a class="big" href="<?php echo APP_URL ?>/item/view/<?php echo $item['id'] ?>" title="点击修改">
<span class="item">
<?php echo ++$number ?>
<?php echo $item['item_name'] ?>
</span>
</a>
----
<a class="big" href="<?php echo APP_URL ?>/item/delete/<?php echo $item['id']?>">删除</a>
<br/>
<?php endforeach ?>
<form action="<?php echo APP_URL ?>/item/update" method="post"> <input type="text" name="value" value="<?php echo $item['item_name'] ?>"> <input type="hidden" name="id" value="<?php echo $item['id'] ?>"> <input type="submit" value="修改"> </form> <a class="big" href="<?php echo APP_URL ?>/item/index">返回</a>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有