手把手编写自己的PHP MVC框架实例教程

WBOY
Release: 2016-06-23 13:08:28
Original
883 people have browsed it

1 什么是MVC

MVC模式 (Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)。

MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并且使程序某一部分的重复利用成为可能。除此之外,此模式通过对复杂度的简化,使程序结构更加直观。软件系统通过对自身基本部份分离的同时也赋予了各个基本部分应有的功能。

简而言之,

  • 模型Model  – 管理所有 数据库 相关的逻辑。模型提供了连接和操作数据库的抽象层。
  • 控制器Controller  - 负责所有的 业务逻辑 ,比如 if/else 逻辑。
  • 视图View  – 负责界面显示,如HMTL/XML/JSON显示。

2 为什么要自己开发MVC框架

网络上有大量优秀的MVC框架可供使用,本教程并不是为了开发一个全面的、终极的MVC框架解决方案,而是将它看作是一个很好的从内部学习PHP的机会,在此过程中,你将学习 面向对象编程 MVC设计模式 ,并学习到开发中的一些注意事项。

更重要的是,你可以完全控制你的框架,并将你的想法融入到你开发的框架中。虽然不一定是做好的,但是你可以按照你的方式去开发功能和模块。

3 开始开发自己的MVC框架

3.1 目录准备

在开始开发前,让我们先来把项目建立好,假设我们建立的项目为 todo,MVC的框架可以命名为 FastPHP,那么接下来的第一步就是把目录结构先设置好。

虽然在这个教程中不会使用到上面的所有的目录,但是为了以后程序的可拓展性,在一开始就把程序目录设置好使非常必要的。下面就具体说说每个目录的作用:

  • application – 应用代码
  • config – 程序配置或数据库配置
  • fastphp - 框架核心目录
  • public – 静态文件
  • runtime - 临时数据目录
  • scripts – 命令行工具

3.2 代码规范

在目录设置好以后,我们接下来就要来规定一下代码的规范:

  1. MySQL的表名需 小写 ,如:item,car
  2. 模块名(Models)需 首字母大写 ,,并在名称后添加“Model”,如:ItemModel,CarModel
  3. 控制器(Controllers)需 首字母大写 ,,并在名称中添加“Controller”,如:ItemsController,CarsController
  4. 视图(Views)部署结构为“控制器名/行为名”,如:item/view.php,car/buy.php

上述的一些规则是为了能在程序中更好的进行互相的调用。接下来就开始真正的PHP MVC编程了。

3.3 重定向

将所有的数据请求都重定向 index.php 文件,在 todo 目录下新建一个 .htaccess  文件,文件内容为:

<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>
Copy after login

这样做的主要原因有:

  1. 程序有一个单一的入口;
  2. 除静态程序,其他所有程序都重定向到 index.php 上;
  3. 可以用来生成利于SEO的URL,想要更好的配置URL,后期可能会需要URL路由,这里先不做介绍了。

3.4 入口文件

做完上面的操作,就应该知道我们需要做什么了,没错!在 public 目录下添加 index.php 文件,文件内容为:

<?php // 应用目录为当前目录define('APP_PATH', __DIR__.'/');// 开启调试模式define('APP_DEBUG', true);// 加载框架require './fastphp/FastPHP.php';
Copy after login

注意,上面的PHP代码中,并没有添加PHP结束符号”?>”,这么做的主要原因是, 对于只有 PHP 代码的文件,结束标志(“?>”)最好不存在,PHP自身并不需要结束符号,不添加结束符号可以很大程度上防止末尾被添加额外的注入内容,让程序更加安全。

3.5 配置文件和主请求

在 index.php 中,我们对 fastphp 文件夹下的 FastPHP.php 发起了请求,那么 FastPHP.php 这个启动文件中到底会包含哪些内容呢?

<?php// 初始化常量defined('ROOT') or define('ROOT', __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/');// 类文件扩展名const EXT = '.class.php';// 包含配置文件require APP_PATH . 'config/config.php';// 包含核心框架类require ROOT . 'Core.php';// 实例化核心类$fast = new Fast;$fast->run();
Copy after login

以上文件都其实可以直接在 index.php 文件中包含,常量也可以直接在 index.php 中定义,我们这么做的原因是为了在后期管理和拓展中更加的方便,所以把需要在一开始的时候就加载运行的程序统一放到一个单独的文件中引用。

先来看看config文件下的 config .php 文件,该文件的主要作用是设置一些程序的配置项及数据库连接等,主要内容为:

<?php /** 变量配置 **/define('DB_NAME', 'todo');define('DB_USER', 'root');define('DB_PASSWORD', 'root');define('DB_HOST', 'localhost');
Copy after login

应该说 config.php 涉及到的内容并不多,不过是一些 基础数据库 的设置,再来看看 fastphp下的共用框架入口文件 Core.php 应该怎么写。

<?php/** * FastPHP核心框架 */class Fast {    // 运行程序    function run() {        spl_autoload_register(array($this, 'loadClass'));        $this->setReporting();        $this->removeMagicQuotes();        $this->unregisterGlobals();        $this->callHook();    }    // 主请求方法,主要目的是拆分URL请求    function callHook() {        if (!empty($_GET['url'])){            $url = $_GET['url'];            $urlArray = explode("/",$url);                        // 获取控制器名            $controllerName = ucfirst(empty($urlArray[0]) ? 'Index' : $urlArray[0]);            $controller = $controllerName . 'Controller';                        // 获取动作名            array_shift($urlArray);            $action = empty($urlArray[0]) ? 'index' : $urlArray[0];                        //获取URL参数            array_shift($urlArray);            $queryString = empty($urlArray) ? array() : $urlArray;        }                // 数据为空的处理        $action = empty($action) ? 'index' : $action;        $queryString  = empty($queryString) ? array() : $queryString;                // 实例化控制器        $int = new $controller($controllerName, $action);            // 如果控制器存和动作存在,这调用并传入URL参数        if ((int)method_exists($controller, $action)) {            call_user_func_array(array($int, $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 = ROOT . $class . EXT;        $controllers = APP_PATH . 'application/controllers/' . $class . EXT;        $models = APP_PATH . 'application/models/' . $class . EXT;        if (file_exists($frameworks)) {            // 加载框架核心类            include $frameworks;        } elseif (file_exists($controllers)) {            // 加载应用控制器类            include $controllers;        } elseif (file_exists($models)) {            //加载应用模型类            include $models;        } else {            /* 错误代码 */        }    }}
Copy after login

下面重点讲解主请求方法 callHook(),首先我们想看看我们的 URL 会这样:

yoursite.com/controllerName/actionName/queryString
Copy after login

callHook()的作用就是,从全局变量 $_GET['url']变量中获取 URL,并将其分割成三部分:$controller、$action 和 $queryString。

例如,URL链接为: todo.com/item/view/1/first-item ,那么

  • $controller 就是: items
  • $action 就是: view
  • 查询字符串Query String就是: array(1, first-item)

分割完成后,会实例化一个新的控制器: $controller.'Controller' (其中“.”是连字符),并调用其方法 $action。

3.6 控制器/Controller基类

接下来的操作就是在 fastphp 中建立程序所需的基类,包括 控制器模型视图 的基类。

新建控制器基类为 Controller.class.php ,控制器的主要功能就是总调度,具体具体内容如下:

<?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 set($name, $value) {        $this->_view->set($name, $value);    }     function __destruct() {        $this->_view->render();    }         }
Copy after login

Controller 类实现所有控制器、模型和视图(View类)的通信。在执行析构函数时,我们可以调用 render() 来显示视图(view)文件。

3.7 模型Model基类

新建模型基类为 Model.class.php ,模型基类 Model.class.php 代码如下:

<?phpclass Model extends Sql {    protected $_model;    protected $_table;     function __construct() {         // 连接数据库        $this->connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);                // 转换模型+Model为模型名称                        // 获取对象所属类的名称        $this->_model = get_class($this);        $this->_model = rtrim($this->_model, 'Model');                // 数据库表名与类名一致        $this->_table = strtolower($this->_model);    }     function __destruct() {    }}
Copy after login

考虑到模型需要对 数据库 进行处理,所以单独建立一个数据库基类 Sql.class.php ,模型基类继承 Sql.class.php,代码如下:

<?php class Sql {    protected $_dbHandle;    protected $_result;    /** 连接数据库 **/    function connect($address, $account, $pwd, $name) {        $this->_dbHandle = @mysql_connect($address, $account, $pwd);        if ($this->_dbHandle != 0) {            if (mysql_select_db($name, $this->_dbHandle)) {                return 1;            }            else {                return 0;            }        }        else {            return 0;        }    }     /** 从数据库断开 **/    function disconnect() {        if (@mysql_close($this->_dbHandle) != 0) {            return 1;        }  else {            return 0;        }    }        /** 查询所有 **/    function selectAll() {        $query = 'select * from `'.$this->_table.'`';        return $this->query($query);    }        /** 根据条件 (id) 查询 **/      function select($id) {        $query = 'select * from `'.$this->_table.'` where `id` = \''.mysql_real_escape_string($id).'\'';        return $this->query($query, 1);    }        /** 根据条件 (id) 删除 **/      function delete($id) {        $query = 'delete from `'.$this->_table.'` where `id` = \''.mysql_real_escape_string($id).'\'';        return $this->query($query);     }         /** 自定义SQL查询 **/    function query($query, $singleResult = 0) {         $this->_result = mysql_query($query, $this->_dbHandle);         if (preg_match("/select/i",$query)) {        $result = array();        $table = array();        $field = array();        $tempResults = array();        $numOfFields = mysql_num_fields($this->_result);        for ($i = 0; $i < $numOfFields; ++$i) { array_push($table,mysql_field_table($this->_result, $i));            array_push($field,mysql_field_name($this->_result, $i));        }                      while ($row = mysql_fetch_row($this->_result)) {                for ($i = 0;$i < $numOfFields; ++$i) { $table[$i] = ucfirst($table[$i]); $tempResults[$table[$i]][$field[$i]] = $row[$i]; } if ($singleResult == 1) { mysql_free_result($this->_result);                    return $tempResults;                }                array_push($result,$tempResults);            }            mysql_free_result($this->_result);            return($result);        }              }     /** 获取记录数 **/    function getNumRows() {        return mysql_num_rows($this->_result);    }     /** 释放查询资源 **/    function freeResult() {        mysql_free_result($this->_result);    }        /** 获取错误信息 **/    function getError() {        return mysql_error($this->_dbHandle);    }    }
Copy after login

应该说, Sql.class.php 是框架的核心部分 。为什么?因为通过它,我们创建了一个 SQL 抽象层,可以大大减少了数据库的编程工作。connect() 和 disconnect() 方法比较简单,不多做说明,重点讲讲 Query查询。假设我们有如下的一段 SQL 查询语句:

SELECT table1.field1, table1.field2, table2.field3, table2.field4 FROM table1,table2 WHERE …
Copy after login

如果使用上面的 SQL 基类,首先要做的工作是选出要输出的字段以及相对应的数据表,然后把它们放到数组中,其中,$field 和 $table 使用相同的索引值。在上面的例子中,它们是这样的:

$field = array(field1,field2,field3,field4);$table = array(table1,table1,table2,table2);
Copy after login

脚本会展开所有的数据行,并将数据表转换成一个模型名(如去除复数和首字母大写)。查询结果最终保存在一个多维数组中,然后返回,格式类似于: $var['modelName']['fieldName'] 。这样输出方式可以非常便于在视图中使用这些元素。

3.8 视图View类

视图类 View.class.php 内容如下:

<?php/** * 视图基类 */class View {         protected $variables = array();    protected $_controller;    protected $_action;         function __construct($controller, $action) {        $this->_controller = $controller;        $this->_action = $action;    }     /** 设置变量方法 **/     function set($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);        }    } }
Copy after login

这样我们的核心的PHP MVC框架就编写完成了,下面我们开始编写应用来测试框架功能。

4 应用

4.1 数据库部署

在 SQL 中新建一个 todo 数据库,使用下面的语句增加 item 数据表并插入2条记录:

CREATE TABLE `items` (    `id` int(11) NOT NULL auto_increment,    `item_name` varchar(255) NOT NULL,    PRIMARY KEY (`id`)); INSERT INTO `items` VALUES(1, 'Hello World.');INSERT INTO `items` VALUES(2, 'Lets go!');
Copy after login

4.2 部署模型

然后,我们还需要在 models 目录中创建一个 ItemModel.php 模型,内容如下:

<?php class ItemModel extends Model {          /** 新增数据 **/    function add($value){         $query = 'insert into `'.$this->_table.'` (item_name) values (\''.mysql_real_escape_string($value).'\')';        return $this->query($query);    }    /** 新增数据 **/    function update($id, $value){        $query = 'update `'.$this->_table.'` set item_name = \''.mysql_real_escape_string($value).'\' where `id` = \''.mysql_real_escape_string($id).'\'';        return $this->query($query);    }    }
Copy after login

模型内容为空。因为 Item 模型继承了 Model,所以它拥有 Model 的所有功能。

4.3 部署控制器

controllers 目录下创建一个 ItemsController.php 控制器,内容如下:

<?php class ItemController extends Controller {        // 首页方法,测试框架自定义DB查询    function index() {        $item = new ItemModel;        $this->set('title', '全部条目');        $this->set('todo', $item->query('select * from item'));    }        // 添加记录,测试框架DB记录创建(Create)    function add() {        $value = $_POST['value'];        $item = new ItemModel;        $this->set('title', '添加成功');        $this->set('todo', $item->add($value));    }        // 查看记录,测试框架DB记录读取(Read)    function view($id = null,$name = null) {        $item = new ItemModel;        $this->set('title', '正在查看'. $name);        $this->set('todo', $item->select($id));    }        // 更新记录,测试框架DB记录更新(Update)    function update() {        $id = $_POST['id'];        $value = $_POST['value'];        $item = new ItemModel;        $this->set('title', '修改成功');        $this->set('todo', $item->update($id, $value));    }        // 删除记录,测试框架DB记录删除(Delete)    function delete($id = null) {        $item = new ItemModel;        $this->set('title','删除成功');        $this->set('todo',$item->delete($id));    } }
Copy after login

4.4 部署视图

views 目录下新建 header.php 和 footer.php 两个页头页脚模板,内容如下。

header.php ,内容:

<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:#222222;            font-family:georgia,times;            font-size:24px;            font-weight:normal;            line-height:1.2em;            color:black;            text-decoration:none;         }         a:hover {            background-color:#BCFC3D;        }        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>
Copy after login

footer.php ,内容:

</body></html>
Copy after login

然后,在 views/item 创建以下几个视图文件。

index.php ,浏览数据库内 item 表的所有记录,内容:

<form action="../item/add" method="post">    <input type="text" value="I have to..." onclick="this.value=''" name="value">    <input type="submit" value="添加"></form><br/><br/><?php $number = 0?> <?php foreach ($todo as $todoitem):?>    <a class="big" href="../../item/view/<?php echo $todoitem['Item']['id']?>/<?php echo strtolower(str_replace(" ","-",$todoitem['Item']['item_name']))?>">        <span class="item">            <?php echo ++$number?>            <?php echo $todoitem['Item']['item_name']?>        </span>    </a>    ----    <a class="big" href="../item/delete/<?php echo $todoitem['Item']['id']?>">删除</a><br/><?php endforeach?>
Copy after login

add.php ,添加记录,内容:

<a class="big" href="../item/">添加成功,点击返回</a>
Copy after login

view.php ,查看单条记录,内容:

<form action="../../../item/update" method="post">    <input type="text" value="<?php echo $todo['Item']['item_name'] ?>" name="value">    <input type="hidden" value="<?php echo $todo['Item']['id'] ?>" name="id">    <input type="submit" value="修改"></form><a class="big" href="../../../item/">返回</a>
Copy after login

update.php ,更改记录,内容:

<a class="big" href="../item/index/">修改成功,点击返回</a>
Copy after login

delete.php ,删除记录,内容:

<a href="../../item/">删除成功,点击返回</a>
Copy after login

4.5 应用测试

这样,在浏览器中访问 todo 程序: http://localhost/todo/item/index/ ,就可以看到效果了。

以上代码全部发布到了我的 github 上,代码仓地址: https://github.com/yeszao/FastPHP ,欢迎克隆、提交。

本文参考:

  • http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!