本文通过实例为大家介绍用php开发一个简单mvc的方法,起到势砖引玉的作用,本文比较适合刚接触mvc的朋友。

MVC其实就是三个Model,Contraller,View单词的简称。

Model,主要任务就是把数据库或者其他文件系统的数据按 照我们需要的方式读取出来。

View,主要负责页面的,把数据以html的形式显示给用户。

Controller,主要负责业务逻辑,根据用户的 Request进行请求的分配,比如说显示登陆界面,就需要调用一个控制器userController的方法loginAction来显示。

本文为大家介绍如何用PHP来创建一个简单的MVC结构系统。

首先创建单点入口,即bootstrap文件index.php,作为整个MVC系统的唯一入口。

什么是单点入口呢?所谓单点入口就是整个应用程序只有一 个入口,所有的实现都通过这个入口来转发。为什么要做到单点入口呢?

单点入口有几大好处:

第一、一些系统全局处理的变量,类,方法都可以在这里进行处理。 比如说你要对数据进行初步的过滤,你要模拟session处理,你要定义一些全局变量,甚至你要注册一些对象或者变量到注册器里面。

第二、程序的架构更加 清晰明了。

include("core/ini.php");

initializer::initialize();

$router = loader::load("router");

dispatcher::dispatch($router);复制代码

这个文件就只有4句,我们现在一句句来分析。

include(”core/ini.php”);

我们来看core/ini.php

set_include_path(get_include_path() . PATH_SEPARATOR . "core/main");

//set_include_path — Sets the include_path configuration option

function __autoload($object){

require_once("{$object}.php");

}复制代码

这个文件首先设置了include_path,也就是我们如果要找包含的文件,告诉系统在这个目录下查找。其实我们定义__autoload()方法,这个方法是在PHP5增加的,就是当我们实例化一个函数的时候,如果本文件没有,就会自动去加载文件。官方的解释是:

Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).

In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

接下来我们看下面一句

initializer::initialize();

这就话就是调用initializer类的一个静态函数initialize,因为我们在ini.php,设置了include_path,以及定义了__autoload,所以程序会自动在core/main目录查找initializer.php.

initializer.php文件如下:

class initializer

{

public static function initialize() {

set_include_path(get_include_path().PATH_SEPARATOR . "core/main");

set_include_path(get_include_path().PATH_SEPARATOR . "core/main/cache");

set_include_path(get_include_path().PATH_SEPARATOR . "core/helpers");

set_include_path(get_include_path().PATH_SEPARATOR . "core/libraries");

set_include_path(get_include_path().PATH_SEPARATOR . "app/controllers");

set_include_path(get_include_path().PATH_SEPARATOR."app/models");

set_include_path(get_include_path().PATH_SEPARATOR."app/views");

//include_once("core/config/config.php");

}

}

?>复制代码

这个函数很简单,就只定义了一个静态函数,initialize函数,这个函数就是设置include_path,这样,以后如果包含文件,或者__autoload,就会去这些目录下查找。

OK,我们继续,看第三句

$router = loader::load(”router”);

这句话也很简单,就是加载loader函数的静态函数load,下面我们来loader.php

class loader

{

private static $loaded = array();

public static function load($object){

$valid = array( "library",

"view",

"model",

"helper",

"router",

"config",

"hook",

"cache",

"db");

if (!in_array($object,$valid)){

throw new Exception("Not a valid object '{$object}' to load");

}

if (empty(self::$loaded[$object])){

self::$loaded[$object]= new $object();

}

return self::$loaded[$object];

}

}复制代码

这个文件就是去加载对象,因为以后我们可能会丰富这个MVC系统,会有model,helper,config等等的组件。如果加载的组件不在有效 的范围内,我们抛出一个异常。如果在的话,我们实例化一个对象,其实这里用了单件设计模式。也就是这个对象其实就只能是一个实例化对象,如果没有实例化, 创建一个,如果存在的,则不实例化。

好,因为我们现在要加载的是router组件,所以我们看下router.php文件,这个文件的作用就是映射URL,对URL进行解析。

router.php

class router

{

private $route;

private $controller;

private $action;

private $params;

public function __construct()

{

$path = array_keys($_GET);

if (!isset($path[0])){

if (!empty($default_controller))

$path[0] = $default_controller;

else

$path[0] = "index";

}

$route= $path[0];

$this->route = $route;

$routeParts = split( "/",$route);

$this->controller=$routeParts[0];

$this->action=isset($routeParts[1])? $routeParts[1]:"base";

array_shift($routeParts);

array_shift($routeParts);

$this->params=$routeParts;

}

public function getAction() {

if (empty($this->action)) $this->action="main";

return $this->action;

}

public function getController() {

return $this->controller;

}

public function getParams() {

return $this->params;

}

}复制代码

我们可以看到,首先我们是拿到$_GET,用户Request的URL,然后从URL里我们解析出Controller和Action,以及Params

比如我们的地址是http://www.tinoweb.cn/user/profile/id/3

那么从上面的地址,我们可以拿到controller是user,action似乎profile,参数是id以及3

OK我们看最后一句,就是

dispatcher::dispatch($router);

这句话的意思很明了,就是拿到URL解析的结果,然后通过dispatcher来分发controlloer及action来Response给用户

好,我们来看下dispatcher.php文件

class dispatcher

{

public static function dispatch($router)

{

global $app;

ob_start();

$start = microtime(true);

$controller = $router->getController();

$action = $router->getAction();

$params = $router->getParams();

$controllerfile = "app/controllers/{$controller}.php";

if (file_exists($controllerfile)){

require_once($controllerfile);

$app = new $controller();

$app->setParams($params);

$app->$action();

if (isset($start)) echo "

Tota1l time for dispatching is : ".(microtime(true)-$start)." seconds.";

$output = ob_get_clean();

echo $output;

}else{

throw new Exception("Controller not found");

}

}

}复制代码

这个类很明显,就是拿到$router来,寻找文件中的controller和action来回应用户的请求。

OK,我们一个简单的,MVC结构,就这样,当然这里还不能算是一个很完整的MVC,因为这里还没有涉及到View和Model,有空我再这里丰富。

我们来写个Controller文件来测试下上面的这个系统。

我们在app/controllers/下创建一个user.php文件

//user.php

class user

{

function base()

{

}

public function login()

{

echo 'login html page';

}

public function register()

{

echo 'register html page';

}

public function setParams($params){

var_dump($params);

}

}复制代码

然后,可以在浏览器中输入http://localhost/index.php?user/register 或 http://localhost/index.php?user/login来测试下。

php开发mvc教程,php开发一个简单的MVC相关推荐

  1. PHP实现MVC开发: 一个简单的MVC(转)

    原地址:http://blog.163.com/zbstrive_work@126/blog/static/165378687201182104617655/ 至于什么MVC结构,其实就是三个Mode ...

  2. Cocos2dx游戏开发系列笔记7:一个简单的跑酷游戏《萝莉快跑》的消化(附下载)

    懒骨头(http://blog.csdn.net/iamlazybone  QQ124774397 青岛 ) 或许有天 我们羡慕和崇拜的人 因为我们的努力 也会来了解我们 说不定 还会成为好友 骨头喜 ...

  3. Cocos2dx游戏开发系列笔记7:一个简单的跑酷游戏《萝莉快跑》的消化(附下载)...

    2019独角兽企业重金招聘Python工程师标准>>> 或许有天 我们羡慕和崇拜的人 因为我们的努力 也会来了解我们 说不定 还会成为好友 骨头喜欢这样与哲哲共勉 多少个夜晚 一张长 ...

  4. Arduino可穿戴开发入门教程Arduino开发环境介绍

    Arduino可穿戴开发入门教程Arduino开发环境介绍 Arduino开发环境介绍 Arduino不像我们使用的PC端操作系统一样,可以直接在操作系统中安装软件为操作系统编程.Arduino的软件 ...

  5. 视频教程-微信小程序开发培训教程-微信开发

    微信小程序开发培训教程 本人计算机专业,毕业工作已经10多年,从事过的行业有,安防,通讯,Gps定位,信息统计分析,互联网电商等,从事过的职位. 代码工程师(使用过的语言C#,PHP,Java),Ap ...

  6. 自己动手写一个简单的MVC框架(第一版)

    一.MVC概念回顾 路由(Route).控制器(Controller).行为(Action).模型(Model).视图(View) 用一句简单地话来描述以上关键点: 路由(Route)就相当于一个公司 ...

  7. 在Java中搭建一个简单的MVC框架

    搭建一个简单的Java MVC框架 一 . 前言 二. 代码实现 1. 思路分析 2. 代码实现 2.1 Controller注解 2.2 RequestMapping注解 2.3 UserContr ...

  8. android studio的GearVR应用开发(二)、一个简单的VR app(Oculus官方GearVR开发教程,翻译转载)

    声明:本文是Oculus官方的GearVR开发教程,为本人翻译转载,供广大VR开发爱好者一同学习进步使用. 原文章 一个简单的VR app 概观 在搭建好GearVR框架后,让我们一起来创建第一个VR ...

  9. [导入]ASP.NET MVC框架开发系列课程(2):一个简单的ASP.NET MVC应用程序.zip(13.70 MB)...

    讲座内容: 使用ASP.NET MVC框架进行开发与ASP.NET WebForms截然不同.本次课程将通过官方的示例程序简单了解一下ASP.NET MVC应用程序的结构与特点. 课程讲师: 赵劼 M ...

最新文章

  1. PMCAFF脉脉:原京东副总裁任鑫教你小团队如何挑战大巨头
  2. 查询计划中集的势(Cardinality)的计算
  3. 轻松记账工程冲刺第二天
  4. ASP.NET Forms验证 实现子域名(SubDomain)共享登陆下的缺陷 [转]
  5. vue引入如何使用不同字体
  6. Android改变图片颜色的自定义控件
  7. script片段在前导致对下文的html元素引用失效
  8. 帆软高级函数应用之数组函数
  9. 配置企业管理系统,什么样的工作流才有用
  10. MATLAB中使用XLSREAD无法找到文件的一种解决方法
  11. D. Take Your Seat
  12. VMware 中Fedora系统连接网络问题!
  13. 关于SharePoint中文翻译的吐槽
  14. JS之 获取日期方法
  15. android关闭传感器,您如何在安卓10手机上打开和关闭传感器
  16. js 混合排序(同时存在数字、字母、汉字等)
  17. Windows10系统时间同步没有效果的解决方法
  18. 如何理解Quorum
  19. uniapp登录授权获取微信手机号组件封装
  20. 学习opengl官方指南 01 opengl介绍

热门文章

  1. 网络基础——知识生活化会变得如此简单
  2. [转]VC++中对文件的写入和读取
  3. Golang 学习笔记(安装)
  4. BIOS interviews
  5. 五类和超五类网线的区别
  6. 什么是加密?—Vecloud微云
  7. oracle中更改列明和更改显示列长度
  8. VC++:如何将程序最小化到托盘
  9. 从命令行运行postman脚本
  10. but no declaration can be found for element #39;aop:aspectj-autoproxy#39;.