您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 盘锦分类信息网,免费分类信息发布

php开发一个简单的MVC

2024/3/26 2:23:05发布7次查看
本文通过实例为大家介绍用php开发一个简单mvc的方法,起到势砖引玉的作用,本文比较适合刚接触mvc的朋友。mvc其实就是三个model,contraller,view单词的简称。model,主要任务就是把数据库或者其他文件系统的数据按 照我们需要的方式读取出来。view,主要负责页面的,把数据以html的形式显示给用户。controller,主要负责业务逻辑,根据用户的 request进行请求的分配,比如说显示登陆界面,就需要调用一个控制器usercontroller的方法loginaction来显示。
本文为大家介绍如何用php来创建一个简单的mvc结构系统。
首先创建单点入口,即bootstrap文件index.php,作为整个mvc系统的唯一入口。什么是单点入口呢?所谓单点入口就是整个应用程序只有一 个入口,所有的实现都通过这个入口来转发。为什么要做到单点入口呢?单点入口有几大好处:第一、一些系统全局处理的变量,类,方法都可以在这里进行处理。 比如说你要对数据进行初步的过滤,你要模拟session处理,你要定义一些全局变量,甚至你要注册一些对象或者变量到注册器里面。第二、程序的架构更加 清晰明了。
复制代码
这个文件就只有4句,我们现在一句句来分析。include(”core/ini.php”);
我们来看core/ini.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文件如下:
复制代码
这个函数很简单,就只定义了一个静态函数,initialize函数,这个函数就是设置include_path,这样,以后如果包含文件,或者__autoload,就会去这些目录下查找。
ok,我们继续,看第三句$router = loader::load(”router”);
这句话也很简单,就是加载loader函数的静态函数load,下面我们来loader.php
复制代码
这个文件就是去加载对象,因为以后我们可能会丰富这个mvc系统,会有model,helper,config等等的组件。如果加载的组件不在有效 的范围内,我们抛出一个异常。如果在的话,我们实例化一个对象,其实这里用了单件设计模式。也就是这个对象其实就只能是一个实例化对象,如果没有实例化, 创建一个,如果存在的,则不实例化。
好,因为我们现在要加载的是router组件,所以我们看下router.php文件,这个文件的作用就是映射url,对url进行解析。router.php
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
复制代码
然后,可以在浏览器中输入http://localhost/index.php?user/register 或 http://localhost/index.php?user/login来测试下。
盘锦分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录