MVC是目前常用的开发架构,基于MVC模式的php开发框架种类繁多,但其原理大同小异。下面粗略的讲解一下MVC中的URL解析过程。
众所周知,MVC架构往往采用单一文件入口,通过URL地址映射,调用相应的controller,model,view完成业务逻辑和页面显示。为了对搜索引擎友好,URL地址往往写成这样:http://localhost/web/action/method/id/1
或者http://localhost/web/action/method/id/1.shtml的形式,即,调用web下的action类中的 method方法,参数是id=1。如何从URL中分离这些类,方法和参数呢?这就需要写一个URL解析类(class.route.php),URL解析完成后,需要调用相应的类完成任务,所以还需要一个分发类(class.dispatcher.php),不要忘了,还有入口文件和配置文件。
一、配置文件:config.php
为了简单起见,我们只配置一下url的伪静态后缀。
<?php
return array(
'
HTML_URL_SUFFIX'=>'
.shtml'
//伪静态后缀shtml,html,htm都可以
);
?>
二、.htaccess文件:过滤掉index.php
# Turn on URL rewriting
RewriteEngine On
# Installation directory 如果你的项目在非根目录的话,就填目录名,比如:/test/
RewriteBase /urltest/
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
# or
#RewriteRule .* index.php?arg=$0 [PT,L]
三、入口文件:index.php
<?php
require '
class.route.php';
require '
class.dispacher.php';
$route =
new Route();
$dis =
new Dispatcher($route);
$dis->dispatch();
?>
四、Route类:用于解析URL
<?php
class Route{
private $action;
private $method;
private $params =
array();
public function __construct(){
$this->parseURL();
}
public function __get($name){
return $name;
}
//URL解析
public function parseURL(){
$str1 = $_SERVER['
PHP_SELF'];
$str2 = $_SERVER['
SCRIPT_NAME'];
$len = strlen($str2);
$des = substr($str1,$len+1,strlen($str1));
$config =
require("
config.php");
if($suffix = $config['
HTML_URL_SUFFIX'])
$des = substr($des,0,strlen($des)-strlen($suffix));
//分别取出action,method,id,1
$arr = explode("
/",$des);
$this->action = array_shift($arr);
$this->method = array_shift($arr);
$this->params =
array();
//参数数组params[参数名称]=参数值
for($i=0;$i<count($arr);$i=$i+2){
$this->params[$arr[$i]] = $arr[$i+1];
}
}
public function getAction(){
return $this->action;
}
public function getMethod(){
return $this->method;
}
public function getParams(){
return $this->params;
}
}
?>
五、Dispatcher类:调用解析出来的类和方法,完成业务逻辑
<?php
class Dispatcher{
private $route = NULL;
private $action;
private $method;
private $params = array();
public function __construct($route){
$this->route = $route;
$this->action = $route->getAction();
$this->method = $route->getMethod();
$this->params = $route->getParams();
}
public function dispatch(){
//加载action类,method方法
if(
empty($this->action))
$this->action="
Index";
if(
empty($this->method))
$this->method = "
index";
if(!file_exists($this->action."
Action.php"))
echo "{$this->action}
Action not found!";
else{
require($this->action."
Action.php");
$className = $this->action."
Action";
$action =
new $className();
$method = $this->method;
if(!method_exists($action,$this->method))
echo "{$method}
() not found";
else
$action->$method($this->params);
}
}
}
?>
六、测试的action类:IndexAction包含inde()方法,用于默认访问
<?php
class IndexAction{
public function index($arr){
echo "
<pre>";
print_r($arr);
echo "
</pre>";
echo "
Hello,world!";
}
}
?>
程序的执行过程是这样的:用户输入http://localhost/web/Index/index/id/1,index.php文件接 收,调用route解析URL地址,dispatcher通过解析结果调用相应的action和method。过程是不是很简单呢,此文只为抱砖引玉,简 述原理。读者可根据自己的需要,根据原理架构自己的MVC框架。
转载于:https://blog.51cto.com/huajie/1155028