写你自己的MVC框架

参考:

 

1. http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/

2. http://anantgarg.com/2009/03/30/write-your-own-php-mvc-framework-part-2/

 

 

 

实例:

 

Controller.php

 

<?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 = '')
	{
		if(empty($method))
		{
			$this->defaultMethod();
		}
		else if(method_exists($this, $method))
		{
			call_user_func(array($this, $method), $params);
		}
		else
		{ 
			$this->noexisting_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 '<br />';
	}
}



$controller = new Controller();
$controller->doAction('link_page', 'b');
$controller->doAction();

 

 

Model.php

 

<?php

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

 

<?php

class View
{
	public function display($output)
	{
		//ob_start();
		echo $output;
	}
}
 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值