自己动手创建简单的MVC框架

// create a virtual host

>mkdir -p /var/www/layne.com/document_root

>cd /etc/apache2/sites-avaliable

>pico -w layne.com 或者 随便建一个文件,用gedit打开,save as layne.com

<VirtualHost *:8060>
        DocumentRoot /var/www/layne.com/document_root
        ServerName layne.com
</VirtualHost>

>a2ensite layne.com

>sudo service apache2 reload

or

>/etc/init.d/apache2 reload

切记:

NameVirtualHost *:8060
Listen 8060

要修改/etc/apache2/ports.conf,加入监听端口,否则无法访问




>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>mkdir -p document_root/images document_root/styles

>mkdir -p application/models application/views application/controllers

>find

>gedit document_root/index.php

hello world!

>gedit document_root/.htaccess

RewriteEngine on
RewriteRule !\.(js|gif|jpg|png|css)$ index.php

//make sure apache trun on mod_rewrite.


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//document_root/index.php
<?php

//import component
require_once('../application/models/front.php');

require_once('../application/models/icontroller.php');

require_once('../application/models/view.php');


//import controller
require_once('../application/controllers/index.php');

//initial front controller
$front = FrontController::getInstance();

$front->route();

echo $front->getBody();

die;



?>

//application/models/front.php
<?php

 class FrontController{
	protected $_controller,$_action,$_params,$_body;
	static $_instance;

	public static function getInstance(){
		if(!self::$_instance instanceof self){
			 self::$_instance = new self();
		}
		return self::$_instance;
	}

	private function __construct(){
		
		try {
			$request = $_SERVER['REQUEST_URI'];
			$splits = explode('/',trim($request,'/'));
			$this->_controller = !empty($splits[0]) ? $splits[0] : 'index';
			$this->_action = !empty($splits[1]) ? $splits[1] : 'index';
			if(!empty($splits[2])){
				$keys = $values = array();
				for($idx = 2; $idx< count($splits); $idx++){
					if($idx%2==0){
						$keys[] = $splits[$idx];
					}else{
						$values[] = $splits[$idx];
					}
				}
				$this->_params = array_combine($keys,$values);
			}
		} catch (Exception $e) {
			throw $e->getMessage();
			return $e->getTrace();
		}
		
			
	}

	public function route(){
		if(class_exists($this->getController())){
			$rc = new ReflectionClass($this->getController());
			if($rc->implementsInterface('IController')){
				if($rc->hasMethod($this->getAction())){
					$controller = $rc->newInstance();
					
					$method = $rc->getMethod($this->getAction());
					$method->invoke($controller);
				}else{
					throw new Exception('Action');
				}
			}else{
				throw new Exception('Interface');
			}
		}else{
			throw new Exception('Controller');
		}
	}

	public function getController(){
		return $this->_controller;
	}

	public function getParams(){
		return $this->_params;
	}

	public function getAction(){
		return $this->_action;
	}

	public function getBody(){
		return $this->_body;
	}

	public function setBody($body){
		$this->_body = $body;
	}

}
?>

//application/models/icontroller.php
<?php

interface IController{


}
?>

//application/models/view.php
<?php
class View extends ArrayObject{
	public function __construct(){
		parent::__construct(array(),ArrayObject::ARRAY_AS_PROPS);
		
	}

	public function render($file){
		ob_start();
		include(dirname(__FILE__). '/' . $file);
		return ob_get_clean();
	}

	
}

?>

//application/controllers/index.php
<?php

class index implements IController{
	public function __construct(){}
	public function index(){
		$fc = FrontController::getInstance();

		$params = $fc->getParams();
		$view = new View();

		$view->name = $params['name'];
		$result = $view->render('../views/index.php');
		$fc->setBody($result);
	}
}
?>

//application/views/index.php
Hello, <?php echo $this->name;?>!

output buffering

点击打开链接http://web.archive.org/web/20101216035343/http://dev-tips.com/featured/output-buffering-for-web-developers-a-beginners-guide


ArrayObject

点击打开链接http://www.php.net/manual/zh/class.arrayobject.php

这里的ArrayObject只是用来存放要set到view中去的对象,当然这些对象你也可以用自定义的容器来存放,比如一个自定义的对象数组。

cakephp里用的是一个单例模式的ClassRegistry的__objects数组来存放的。

function addObject($key, &$object) {
		$_this =& ClassRegistry::getInstance();
		$key = Inflector::underscore($key);
		if (!isset($_this->__objects[$key])) {
			$_this->__objects[$key] =& $object;
			return true;
		}
		return false;
	}

















你可能感兴趣的:(mvc)