什么是请求分发器呢?
观察几段代码:
如下:
上面的代码都如下特性:
xxx+Controller
xxx+Model
有没有可能,使用一个参数,在每次请求的时候,都带上“要使用的”控制器名?
例如:点击产品列表,我们跳转链接中链接是否可以带上ProductController名呢?
如果可以,则,我们就可以将代码进一步“提升”(简化为):
$c = “User”; //它也可能是“product”,或其他。。。
require ‘./” . $c . “Model.class.php’;
require './ModelFactory.class.php'; //这个都一样,不同动
require './BaseController.class.php';//这个都一样,不同动
class XXXController extends BaseController{ ...... } //代表某个控制器,每个控制器是一个独立文件
require ‘./’ . $c . “Controller.class.php”; //这里才是需要载入的“当前控制器类”
$controller_name = $c . “Controller”; 构建控制器的类名
$ctrl = new $controller_name (); //可变类
$act = !empty($_GET['a']) ? $_GET['a'] : "Index";
$action = $act . "Action";
$ctrl->$action();
理解如下:
当我们点击产品列表时,
我们$c=Product->
这句代码:require ‘./” . $c . “Model.class.php’; 转化成 require './ProductModel.class.php';
这句代码:require ‘./’ . $c . “Controller.class.php”; 转化成require'.ProductController.class.php';
$ctrl = new $controller_name (); 转化成$ctrl=new ProductController();
我觉得,$c就像圣旨,圣旨到,控制器更像是将军,拥兵百万,准备战斗。
例如你是杨凌,皇上命你为神机营的将军,你就可以管理神机营
皇上命你杨凌为内厂总督,你就只能是管内厂,你会发现杨凌还是那个杨凌,但只是你的官称不一样。
下面代码如下:
index.php(你可以把这个文件看做杨凌)
<?php
$c=!empty($_GET['c'])?$_GET['c']:"User";
require './'.$c.'Model.class.php';//模型文件
require './ModelFactory.class.php';
require'./BaseController.class.php';
require './'.$c.'Controller.class.php';
$controller_name=$c."Controller";
$ctrl=new $controller_name();
$act=!empty($_GET['act'])?$_GET['act']:"Index";
$action=$act."Action";
$ctrl->$action();
?>
showAllUser_view.html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>网页标题</title>
<meta name="keywords" content="关键字列表" />
<meta name="description" content="网页描述" />
<link rel="stylesheet" type="text/css" href="" />
<style type="text/css">
.t1{color:red;}
.t2{background:#cccccc}
a{
text-decoration: none;
color:red;
}
</style>
<script type="text/javascript"></script>
</head>
<body>
<a href="?c=Product&act=ShowAllproduct">产品列表</a>
<hr />
<a href="?c=User">用户列表</a>
<hr />
<a href="?act=ShowForm">添加新用户</a>
<hr/>
用户列表:
<table border='1'>
<?php
foreach( $data1 as $key => $rec )
{
?>
<tr>
<td><?php echo $rec['user_name']; ?></td>
<td class='t1'><?php echo $rec['age']; ?></td>
<td><?php echo $rec['edu']; ?></td>
<td class='t2'><?php echo $rec['xingqu']; ?></td>
<td><?php echo $rec['from']; ?></td>
<td><?php echo $rec['reg_time']; ?></td>
<td>
<a href='?c=User&act=del&id=<?php echo $rec['user_id']; ?>' onclick='return queren()'>删除</a>
<a href='?c=User&act=detail&id=<?php echo $rec['user_id']; ?>' >详细</a>
<a href='?c=User&act=edit&id=<?php echo $rec['user_id']; ?>' >修改</a>
</td>
</tr>
<?php
}
?>
</table>
<br />
当前用户总数:<?php echo $data2;?>
</body>
</html>
<script>
function queren(){
return window.confirm("你真的要删除吗?");
}
</script>
form_view.html代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>网页标题</title>
<meta name="keywords" content="关键字列表" />
<meta name="description" content="网页描述" />
<link rel="stylesheet" type="text/css" href="" />
<style type="text/css"></style>
<script type="text/javascript"></script>
</head>
<body>
<form name="form1" action="?c=User&act=addUser" method="post">
<h1>添加数据</h1>
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
年龄:<input type="text" name="age"><br/>
学历:<select name="edu">
<option value="1">小学</option>
<option value="2">中学</option>
<option value="3">大学</option>
<option value="4">硕士</option>
<option value="5">博士</option>
</select><br/>
兴趣爱好:
<input type="checkbox" name="aihao[]" value="1" >排球
<input type="checkbox" name="aihao[]" value="2">篮球
<input type="checkbox" name="aihao[]" value="4">足球
<input type="checkbox" name="aihao[]" value="8">中国足球
<input type="checkbox" name="aihao[]" value="16">地球<br/>
来自:
<input type="radio" name='from' value="1">东北
<input type="radio" name='from' value="2">华北
<input type="radio" name='from' value="3">西北
<input type="radio" name='from' value="4">华东
<input type="radio" name='from' value="5">华北
<input type="radio" name='from' value="6">华西<br/>
<input type="submit" value="提交" >
</form>
</body>
</html>
ProductController.class.php代码:
<?php
//require './ProductModel.class.php';
//require './ModelFactory.class.php';
//require'./BaseController.class.php';
class ProductController extends BaseController{
function ShowAllProductAction(){
$obj_user = ModelFactory::M('ProductModel');
$data1 = $obj_user->GetAllProduct(); //是一个二维数组
// $data2 = $obj_user->GetUserProduct(); //是一个数字
include './Product_list.html';
}
function DetailAction(){
}
function DelAction(){
}
}
//$ctrl=new ProductController();
//$act=!empty($_GET['act'])?$_GET['act']:"Index";
//$action=$act."Action";
//$ctrl->$action();
?>
User以此类推
这就是,所谓的前端控制器(请求分发器):
它的作用是:
1,根据传过来的c请求数据,决定使用哪个控制器;——有默认值,目前为User
2,根据传过来的a请求数据,决定使用哪个动作(方法);——有默认值,目前为Index
而且,此时,UserController类和ProductController类中,没有其他代码了,只有“纯类”的定义代码;