php唯一入口,PHP整合你的站点入口

大家也许经常在网上看到这样的路径(http://www.aaa.com/aaa/bbb/aaa?id=5),让人不解,这样的的实现方式有几种可能性:

1、隐藏文件的扩展名,对这种做法的好处,众说纷纭,不过个人觉得没有必要;

2、用了的重定向规则,实现虚拟路径;

3、强制文件解析的方式,实现虚拟路径。

用第2/3种方法可以实现的统一接口,合理的整合,更好的体现的安全性和架构,用这两种方式的大多是使用"MVC"模式构建和实现的。

下面是一个例子

访问路径如下:

....../test/*******/Bad

....../test/*******/Good

(其中的"******"可以用任何字符串替换,"......."是你的web路径)

文件的目录结构如下

|--.ht

|-- test

|-- Application.php

|-- Controler/GoodControler.php

|-- Controler/BadControler.php

注意文件".ht",在windows下不能直接建立的,可以在命令行模式下建立.

文件0 (.ht) (这个文件是更改apache的配置方式用的)

forcetype application/x-httpd-php

文件1 (test.php)

/*-------------------------------------

* test.php

*

* 作为你的的入口的文件

* 用来初始化和入口

* 调用执行Controler的调用

*

-------------------------------------*/

require "Application.php";

$aa = new Application();

$aa->parse();

$aa->go();

?>

文件2 (GoodControler.php)

/*-------------------------------------

* GoodControler.php

*

* 用来控制 url=/test/Good 来的访问

*

-------------------------------------*/

class GoodControler{

/*

* 控制类的调用方法,唯一的报漏给外部的接口

*/

function control(){

echo "this is from GoodControler url=*********/test/Good";

}

}

?>

文件3 (BadControler.php)

/*-------------------------------------

* BadControler.php

*

* 用来控制 url=/test/Bad 来的访问

*

-------------------------------------*/

class BadControler{

/*

* 控制类的调用方法,唯一的报漏给外部的接口

*/

function control(){

echo "this is from GoodControler url=*********/test/Bad";

}

}

?>

文件4 (Application.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();

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值