关于模块式开发与MVC开发的小结
时间:2009-07-11
来源:互联网
今天,看了一些关于php开发的文章,做了一些实验,觉得有必要把php网站模块化开发,以及MVC结构的开发作一个小结,并给出相应的结论...
首先是php模块化开发,首先看以下一个例子
首先在一个Root目录下,建立以下的东西
--includes--config.inc.php
--header.html
--footer.html
--index.php
--modules--main.inc.php
以下给出.部分文件的内容
config.inc.php[code]
<?php # - config.inc.php
/*
* File name: config.inc.php
* Created by: 赵斌全
* Contact: [email protected]
* Last modified:
*
* Configuration file does the following things:
* - Has site settings in one location. 站点设置
* - Stores URLs and URIs as constants. URL等设置
* - Sets how errors will be handled. 错误处理
*/
// 设置联系邮箱
$contact_email = '[email protected]';
//设置是本地测试还是服务器上的连接
if (stristr($_SERVER['HTTP_HOST'], 'local') || (substr($_SERVER['HTTP_HOST'], 0, 7) == '192.168')) {
$local = TRUE;
} else {
$local = FALSE;
}
//本地或服务器的不同处理
if ($local) {
// 本地一定是调试状态
$debug = TRUE;
// 定义基本参数
define ('BASE_URI', '/path/to/html/folder/');
define ('BASE_URL', 'http://localhost/cms/');
define ('DB', '/path/to/mysql.inc.php');
} else {
define ('BASE_URI', '/path/to/live/html/folder/');
define ('BASE_URL', 'http://www.example.com/');
define ('DB', '/path/to/live/mysql.inc.php');
}
/*
*
用$debug控制,是否include('config.inc.php');
*
*/
if (!isset($debug)) {
$debug = FALSE;
}
// 错误处理函数
public function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars)
{
global $debug, $contact_email;
// 通用信息
$message = "错误发生在'$e_file' 第 $e_line: 行\n<br />$e_message\n<br />";
$message .= "时间: " . date('n-j-Y H:i:s') . "\n<br />";
$message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />";
if ($debug) {
echo '<p class="error">' . $message . '</p>';
} else {
error_log ($message, 1, $contact_email); // Send email.
if ( ($e_number != E_NOTICE) && ($e_number < 2048)) {
echo '<p class="error">一个系统错误发生,我们对造成您的不方便表示歉意.</p>';
}
} // End of $debug IF.
} // End of my_error_handler() definition.
// 使用自定义的错误处理函数
set_error_handler ('my_error_handler');
?>
[/code]header.html文件为空
index.php[code]<?php # Script 2.4 - index.php
/*
* This is the main page.
* This page includes the configuration file,
* the templates, and any content-specific modules.
*/
// Require the configuration file before any PHP code:
require_once ('./includes/config.inc.php');
// Validate what page to show:
if (isset($_GET['p'])) {
$p = $_GET['p'];
} elseif (isset($_POST['p'])) { // Forms
$p = $_POST['p'];
} else {
$p = NULL;
}
// Determine what page to display:
switch ($p) {
case 'about':
$page = 'about.inc.php';
$page_title = 'About This Site';
break;
case 'this':
$page = 'this.inc.php';
$page_title = 'This is Another Page.';
break;
case 'that':
$page = 'that.inc.php';
$page_title = 'That is Also a Page.';
break;
case 'contact':
$page = 'contact.inc.php';
$page_title = 'Contact Us';
break;
case 'search':
$page = 'search.inc.php';
$page_title = 'Search Results';
break;
// Default is to include the main page.
default:
$page = 'main.inc.php';
$page_title = 'Site Home Page';
break;
} // End of main switch.
// Make sure the file exists:
if (!file_exists('./modules/' . $page)) {
$page = 'main.inc.php';
$page_title = 'Site Home Page';
}
// Include the header file:
include_once ('./includes/header.html');
// Include the content-specific module:
// $page is determined from the above switch.
include ('./modules/' . $page);
// Include the footer file to complete the template:
include_once ('./includes/footer.html');
?>
[/code]main.inc.php为空
这样就把一个模块化网站的雏形搭建好了....
然后是,MVC形式网站的搭建
重新使用一个Root目录
建立index.php
conf.inc.php
data.inc.php
controller.inc.php
内容如下:
index.php[code] <?
define('IN_MP', true);
define('ROOT',realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
error_reporting(E_ALL & ~E_NOTICE);
ob_start();
header("Content-type: text/html; charset=gb2312");
session_start();
// base
class base{
// 配置
protected $config;
// 加载配置
protected function init(){
$this->config = & $GLOBALS['config'];
}
// 抛出异常
protected function throwError($error='error!'){
throw new Exception($error);
}
// 处理异常
protected function dealError($e){
die($e->getMessage());
}
}
// app
class app extends base{
// 方法
protected $action = 'index';
// 参数
protected $param = '';
// 构造方法
public function __construct(){
parent::init();
}
// 执行
public function run(){
try{
$this->bulid();
//var_export($_GET);
$this->exec();
} catch(Exception $e){
$this->dealError($e);
}
}
// 分析URL请求
protected function bulid(){
$query = $_SERVER["QUERY_STRING"];
$get = explode('-',$query);
if($_GET['action']){
$this->config->action = $_GET['action'];
} elseif(!empty($get[0])){
$this->config->action = $get[0];
}
if($_GET['param']){
$this->config->param = $_GET['param'];
} elseif(!empty($get[1])){
$this->config->param = $get[1];
}
}
// 控制器
protected function exec(){
$controller = new controller();
if(method_exists($controller, $this->config->action)){
$action = $this->config->action;
$controller->$action();
} else{
$this->throwError($this->config->action.': 指定的方法不存在!');
}
}
}
//配置文件
require_once('conf.inc.php');
require_once("controller.inc.php");
require_once("data.inc.php"); //在不同文件中,代码要放在类运行之前;同一文件中,则可以在类运行后
// 运行项目
$app = new app();
$app->run();
?>
[/code]conf.inc.php内容[code]<?
// 配置
$config = new stdClass();
/* 这里开始可以自定义 */
$config->author = '小鱼'; // 网站主人
$config->pwd = 'admin'; // 管理密码
$config->title = '小鱼的心情'; // 标题
$config->write = '如果目标是地平线,留给世界的就只能是背影!'; // 个性签名
$config->listNum = 10; // 每页显示数
$config->dataDir = '../data/'; // 数据目录
$config->viewDir = '../view/'; // 模板目录
$config->guestPost = true; // 允许游客发表
$config->moods = array(
'says'=>'说',
'asks'=>'问到',
);
/* 自定义到这里结束 */
$config->action = 'index';
$config->param = NULL;
$config->isLogin = false;
?>[/code]data.inc.php[code] <?
// data
class data extends base {
protected $dataTotal;
// 构造方法
public function __construct(){
parent::init();
if(!is_dir($this->config->dataDir)){
$this->throwError($this->config->dataDir.': 数据目录不存在!');
}
}
}
?>
[/code]controller.inc.php内容[code]<?
// controller
class controller extends base {
protected $data;
// 构造方法
public function __construct(){
parent::init();
$this->data = new data();
}
// 首页
public function index(){
echo "index中文";
}
}
?>[/code]大功告成了,这样,MVC网站形式的雏形也搭建好了...
当你把上述的两种结构读完后,你会发现:
很久以前,我们会采用了第一种的方式对网站进行了架构...而这样的方式,具体体现在switch所指向的不同文件,也即所谓的模块开发
而采用了第二种的MVC结构后,网站被分成三个体系结构.不同的结构处理不同的action
作者: chnzbq 发布时间: 2009-07-11
作者: zhangchen2397 发布时间: 2009-07-11
作者: alei817927 发布时间: 2009-07-11
作者: Alog_W 发布时间: 2009-07-13
作者: chnzbq 发布时间: 2009-07-16
作者: luzhou 发布时间: 2009-07-16
作者: chnzbq 发布时间: 2009-07-23
我觉得类里面所有属性都不应该是public的,需要访问的话,通过自定义方法实现。
作者: mycyndi 发布时间: 2009-07-23
我觉得类里面所有属性都不应该是public的,需要访问的话,通过自定义方法实现。
mycyndi 发表于 2009-7-23 13:30
作者: chnzbq 发布时间: 2009-07-31
作者: cyhchenz 发布时间: 2009-08-01
作者: sychen 发布时间: 2009-08-03
作者: wzwen 发布时间: 2009-08-03
作者: Sogili 发布时间: 2009-08-05

作者: chnzbq 发布时间: 2009-08-16

作者: yua3891 发布时间: 2009-08-21
作者: dyl213 发布时间: 2009-08-21
作者: huahuatongxue 发布时间: 2009-09-07
作者: chnzbq 发布时间: 2009-10-04
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28