下面是一个超级简单的MVC结构实现,甚至连数据源都用了一个内置的固定数组,虽然简单,但其实众多的PHP Framework核心实现的思想应该和这个是差不多的
只不过一些framework提供了更多的方便开发者使用的工具,我也想自己来实现一个php的 框架,目前正在着手策划中,也希望自己能够从框架的开发中学习到更多的php设计思想和方法。
Controller.php
include 'Model.php';
include 'View.php';
class Controller {
private $model = '';
private $view = '';
public function Controller(){
$this->model = new Model();
$this->view = new View();
}
public function doAction( $method = 'defaultMethod', $params = array() ){
if( empty($method) ){
$this->defaultMethod();
}else if( method_exists($this, $method) ){
call_user_func(array($this, $method), $params);
}else{
$this->nonexisting_method();
}
}
public function link_page($name = ''){
$links = $this->model->getLinks();
$this->view->display($links);
$result = $this->model->getResult($name);
$this->view->display($result);
}
public function defaultMethod(){
$this->br();
echo "This is the default method. ";
}
public function nonexisting_method(){
$this->br();
echo "This is the noexisting method. ";
}
public function br(){
echo "
";
}
}
$controller = new Controller();
$controller->doAction('link_page', 'b');
$controller->doAction();
Model.php
Code
class Model {
private $database = array(
"a" => "hello world",
"b" => "ok well done",
"c" => "good bye",
);
//@TODO connect the database
//run the query and get the result
public function getResult($name){
if( empty($name) ){
return FALSE;
}
if( in_array($name, array_keys( $this->database ) ) ){
return $this->database[$name];
}
}
public function getLinks(){
$links = "Link A ";
$links.= "Link B ";
$links.= "Link C ";
return $links;
}
}
View.php
系统简介:冰兔BToo网店系统采用高端技术架构,具备超强负载能力,极速数据处理能力、高效灵活、安全稳定;模板设计制作简单、灵活、多元;系统功能十分全面,商品、会员、订单管理功能异常丰富。秒杀、团购、优惠、现金、卡券、打折等促销模式十分全面;更为人性化的商品订单管理,融合了多种控制和独特地管理机制;两大模块无限级别的会员管理系统结合积分机制、实现有效的推广获得更多的盈利!本次更新说明:1. 增加了新
class View {
public function display($output){
// ob_start();
echo $output;
}
}










