本文介绍如何用php来实现mvc模式开发的文件。关于mvc模式的技术文章网上随处可以,所以这篇文件将不再讲述这种模式的优缺点(实际上是我说不清楚),只讲他的php技术实现。并且在以后的系列文章中也是以讲技术为主。
一、实现统一的网站入口(在mvc中调用controler层的方法,也就是控制层)
大家也许经常在网上看到这样的路径(http://www.aaa.com/aaa/bbb/aaa?id=5),让人不解,这样的网站的实现方式有几种可能性:
1、隐藏文件的扩展名,对这种做法的好处,众说纷纭,不过个人觉得没有必要;
2、用了网站的重定向规则,实现虚拟路径;
3、强制文件解析的方式,实现虚拟路径。
用第23种方法可以实现网站的统一接口,合理的整合网站,更好的体现网站的安全性和架构,用这两种方式的网站大多是使用“mvc”模式构建和实现的。
下面是一个例子
访问路径如下:
....../test/*******/bad
....../test/*******/good
(其中的******可以用任何字符串替换,.......是你的web路径)
文件的目录结构如下
|-- .htaccess
|-- test
|-- application.php
|-- controler/goodcontroler.php
|-- controler/badcontroler.php
注意 文件.htaccess,在windows下不能直接建立的,可以在命令行模式下建立.
文件0:(.htaccess)(这个文件是更改apache的配置方式用的)
forcetype application/x-httpd-php
文件 test.php
parse();
$aa->go();
?>
文件goodcontroler.php
文件 badcontroler.php
文件 application.php
_parsepath();
$this->_getcontrolerfile();
$this->_getcontrolerclassname();
}
/*
* 解析当前的访问路径,得到要进行动作
*/
function _parsepath(){
list($path, $param) = explode(?, $_server[request_uri]);
$pos = strrpos($path, /);
$this->action = substr($path, $pos 1);
}
/*
* 通过动作$action,解析得到该$action要用到的controler文件的路径
*/
function _getcontrolerfile(){
$this->controlerfile = ./controler/.$this->action.controler.php;
if(!file_exists($this->controlerfile))
die(controler文件名(.$this->controlerfile.)解析错误);
require_once $this->controlerfile;
}
/*
* 通过动作$action,解析得到该$action要用到的controler类名
*/
function _getcontrolerclassname(){
$this->controlerclass = $this->action.controler;
if(!class_exists($this->controlerclass))
die(controler类名(.$this->controlerclass.)解析错误);
}
/*
* 调用controler,执行controler的动作
*/
function go(){
$c = new $this->controlerclass();
$c->control();
}
}
?>
下节讲继续讲解php 中 mvc模式开发。
http://www.bkjia.com/phpjc/486163.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/486163.htmltecharticle本文介绍如何用php来实现mvc模式开发的文件。关于mvc模式的技术文章网上随处可以,所以这篇文件将不再讲述这种模式的优缺点(实际上是...