程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> 簡單的PHP框架

簡單的PHP框架

編輯:PHP基礎知識
 

簡單的PHP MVC框架

\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() {
$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, $cnt = count($splits); $idx<$cnt; $idx++) {
if($idx % 2 == 0) {
//Is even, is key
$keys[] = $splits[$idx];
} else {
//Is odd, is value;
$values[] = $splits[$idx];
}
}
$this->_params = array_combine($keys, $values);
}
}

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 getParams() {
return $this->_params;
}

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

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

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

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

}
\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\models\icontroller.php

代碼如下
<?php

interface IController {}

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved