自己动手创建简单的MVC框架
来源:互联网 发布:java项目简介怎么写 编辑:程序博客网 时间:2024/06/05 16:45
// 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 reloador>/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.phphello world!>gedit document_root/.htaccessRewriteEngine onRewriteRule !\.(js|gif|jpg|png|css)$ index.php//make sure apache trun on mod_rewrite.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//document_root/index.php<?php//import componentrequire_once('../application/models/front.php');require_once('../application/models/icontroller.php');require_once('../application/models/view.php');//import controllerrequire_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<?phpinterface IController{}?>
//application/models/view.php<?phpclass 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<?phpclass 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.phpHello, <?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框架
- 自己动手写一个简单的MVC框架(第一版)
- 自己动手写一个简单的MVC框架(第二版)
- 自己动手写一个简单的MVC框架(第二版)
- 自己动手写MVC框架
- 自己动手写一个简单的mvc框架(一)前期准备
- 自己动手写PHP MVC框架
- 自己动手写PHP MVC框架
- 自己动手写PHP MVC框架
- 自己动手写PHP MVC框架
- 简单的mvc 框架(五)
- MVC框架的简单描述
- 封装简单的mvc框架
- 《自己动手写框架4》:分布式锁的简单实现
- 4.自己动手写Java Web框架-MVC++
- java 自己动手做框架之MVC
- 自己动手模仿 springmvc 写一个 mvc框架
- 自己动手模仿 springmvc 写一个 mvc框架
- 专访李民录:主流语言C++入门并不难
- KinectFusion--3D重建的利器
- oracle select执行顺序
- JFreeChart中文API
- c语言第二堂
- 自己动手创建简单的MVC框架
- C语言——归并排序练习题
- unity自带LightMapping和3Ds Max烘焙贴图的小对比
- 我的第一个Servlet(一)——搭建环境
- schemaExport create 方法参数
- oracle Move空间释放
- Android-StatusBar+Notification
- android 在xml文件中引用自定义View
- Unity3D 换贴图脚本