用PHP开始你的MVC (一)整合你的站点入口(2)
文件2:(GoodControler.php)
<?php
/*-------------------------------------
* GoodControler.php
*
* 用来控制 url=/test/Good 来的访问
*
-------------------------------------*/
class GoodControler{
/*
* 控制类的调用方法,唯一的报漏给外部的接口
*/
function control(){
echo "this is from GoodControler url=*********/test/Good";
}
}
?>
文件3:(BadControler.php)
<?php
/*-------------------------------------
* BadControler.php
*
* 用来控制 url=/test/Bad 来的访问
*
-------------------------------------*/
class BadControler{
/*
* 控制类的调用方法,唯一的报漏给外部的接口
*/
function control(){
echo "this is from GoodControler url=*********/test/Bad";
}
}
?>
文件4:(Application.php)
<?php
/*-------------------------------------
* Application.php
*
* 用来实现网站的统一入口,调用Controler类
*
-------------------------------------*/
class Application{
//用来记录所要进行的操作
var $action;
//controler文件的路径名
var $controlerFile;
//controler的类名
var $controlerClass;
function Application(){
}
function parse(){
$this->_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();
}
}
?>