先來張主體架構圖吧
可以看出來昨天做的load.php這隻程式的重要性了吧(處理url)今天要進入MVC的主題了
先在根目錄下新增幾個資料夾:
view
control
lib
然後修改下load.php這隻程式
<?php
if(!isset($_SESSION))
session_start();
$UrlBase = explode('load.php',$_SERVER['PHP_SELF']);
if(!isset($_SESSION['SiteUrl'])){
$port = ($_SERVER['SERVER_PORT'] == 80)?'http://':'https://';
$_SESSION['SiteUrl'] = $port . $_SERVER['HTTP_HOST'] . $UrlBase[0];
}
$url = explode('/',$UrlBase[1]);
include 'lib/LibBoot.php';
$boot = new LibBoot($url);
?>
沒錯,就是要把接下來的流程導向LibBoot.php這隻分配的程式去(把load.php產生的url這個array丟進去)if(!isset($_SESSION))
session_start();
$UrlBase = explode('load.php',$_SERVER['PHP_SELF']);
if(!isset($_SESSION['SiteUrl'])){
$port = ($_SERVER['SERVER_PORT'] == 80)?'http://':'https://';
$_SESSION['SiteUrl'] = $port . $_SERVER['HTTP_HOST'] . $UrlBase[0];
}
$url = explode('/',$UrlBase[1]);
include 'lib/LibBoot.php';
$boot = new LibBoot($url);
?>
並且產一個叫SiteUrl的SESSION,這個東西後面會用到
再來在lib這個資料夾底下產生一個叫LibBoot.php這隻程式
打開他
<?php
class LibBoot {
function __construct($url) {
//檢查是否不用載入view
$cgi = (isset($url[0]))?in_array($url[0],array('cgi','js')):false;
$view = false;
if(!$cgi){
//設定url[0]和url[1]
$url[0] = (!isset($url[0]) or $url[0] == '')?'index':$url[0];
$url[1] = (!isset($url[1]) or $url[1] == '')?'index':$url[1];
}else{
//是cgi就把url[0]之後的向前搬
if($url[0] == 'cgi'){
$url[0] = $url[1];
if(isset($url[2]))
$url[1] = $url[2];
$url[0] = (!isset($url[0]) or $url[0] == '')?'index':$url[0];
$url[1] = (!isset($url[1]) or $url[1] == '')?'index':$url[1];
}
//是js,就建立url[1](如果url[1]存在就搬進去,沒有就用Jquery)
if($url[0] == 'js'){
$url[1] = (!isset($url[1]) or $url[1] == '')?'Jquery':$url[1];
}
}
//檢查control的class是不是存在
$control = $this->FileCk(SCANDIR('control'), $url[0]);
if(!$cgi){
//檢查view的資料夾是不是存在
$view = $this->FileCk(SCANDIR('view'), $url[0],false);
//檢查view的檔案是不是存在
$view = $this->FileCk(SCANDIR("view/$view"), $url[1]);
if($control == 'error' or $view == 'error'){
$view = '404';
$control = 'error';
}
}
/*control start*/
include "control/$control.php";
$ControlObj = new $control;
//接control吐出來的資料
$ControlRet = array();
//傳入的資料替代
$data['get'] = $this->InDataCk($_GET);
$data['post'] = $this->InDataCk($_POST);
//include js
if($url[0] == 'js'){
$data = $url[1];
$url[1] = 'getJs';
}
//檢查function是否在coltrol中
if (method_exists($ControlObj, $url[1])) {
if(
(isset($data['get']) and count($data['get']) != 0) or
(isset($data['post']) and count($data['post']) != 0) or
$url[1] == 'getJs'
){
$ControlRet = $ControlObj->{$url[1]}($data);
}else{
$ControlRet = $ControlObj->{$url[1]}();
}
}
/*control end*/
/*view start*/
if($view){
include "view/View.php";
$ViewObj = new View($control .'/'.$view,$ControlRet);
}
/*view end*/
}
private function FileCk($arr, $file_name, $isFile = true) {
$ret = 'error';
foreach ($arr as $value) {
if (substr($value, 0, strrpos($value, ".")) == $file_name and $isFile)
$ret = $file_name;
if($value == $file_name and !$isFile)
$ret = $file_name;
}
return $ret;
}
private function InDataCk($arr) {
$data = array();
foreach ($arr as $key => $value) {
$data[$key] = $this->CheckInput($value);
}
return $data;
}
private function CheckInput($Indata) {
if (is_array($Indata)) {
foreach ($Indata as $key => $value)
$Indata[$key] = $this->CheckInput($value);
} else {
$Indata = str_replace(array("&", "'", '"', "<", ">"), array('@&5', '@&1', '@&2', '@&3', '@&4'), $Indata);
}
return $Indata;
}
}
基本上只有四隻functionclass LibBoot {
function __construct($url) {
//檢查是否不用載入view
$cgi = (isset($url[0]))?in_array($url[0],array('cgi','js')):false;
$view = false;
if(!$cgi){
//設定url[0]和url[1]
$url[0] = (!isset($url[0]) or $url[0] == '')?'index':$url[0];
$url[1] = (!isset($url[1]) or $url[1] == '')?'index':$url[1];
}else{
//是cgi就把url[0]之後的向前搬
if($url[0] == 'cgi'){
$url[0] = $url[1];
if(isset($url[2]))
$url[1] = $url[2];
$url[0] = (!isset($url[0]) or $url[0] == '')?'index':$url[0];
$url[1] = (!isset($url[1]) or $url[1] == '')?'index':$url[1];
}
//是js,就建立url[1](如果url[1]存在就搬進去,沒有就用Jquery)
if($url[0] == 'js'){
$url[1] = (!isset($url[1]) or $url[1] == '')?'Jquery':$url[1];
}
}
//檢查control的class是不是存在
$control = $this->FileCk(SCANDIR('control'), $url[0]);
if(!$cgi){
//檢查view的資料夾是不是存在
$view = $this->FileCk(SCANDIR('view'), $url[0],false);
//檢查view的檔案是不是存在
$view = $this->FileCk(SCANDIR("view/$view"), $url[1]);
if($control == 'error' or $view == 'error'){
$view = '404';
$control = 'error';
}
}
/*control start*/
include "control/$control.php";
$ControlObj = new $control;
//接control吐出來的資料
$ControlRet = array();
//傳入的資料替代
$data['get'] = $this->InDataCk($_GET);
$data['post'] = $this->InDataCk($_POST);
//include js
if($url[0] == 'js'){
$data = $url[1];
$url[1] = 'getJs';
}
//檢查function是否在coltrol中
if (method_exists($ControlObj, $url[1])) {
if(
(isset($data['get']) and count($data['get']) != 0) or
(isset($data['post']) and count($data['post']) != 0) or
$url[1] == 'getJs'
){
$ControlRet = $ControlObj->{$url[1]}($data);
}else{
$ControlRet = $ControlObj->{$url[1]}();
}
}
/*control end*/
/*view start*/
if($view){
include "view/View.php";
$ViewObj = new View($control .'/'.$view,$ControlRet);
}
/*view end*/
}
private function FileCk($arr, $file_name, $isFile = true) {
$ret = 'error';
foreach ($arr as $value) {
if (substr($value, 0, strrpos($value, ".")) == $file_name and $isFile)
$ret = $file_name;
if($value == $file_name and !$isFile)
$ret = $file_name;
}
return $ret;
}
private function InDataCk($arr) {
$data = array();
foreach ($arr as $key => $value) {
$data[$key] = $this->CheckInput($value);
}
return $data;
}
private function CheckInput($Indata) {
if (is_array($Indata)) {
foreach ($Indata as $key => $value)
$Indata[$key] = $this->CheckInput($value);
} else {
$Indata = str_replace(array("&", "'", '"', "<", ">"), array('@&5', '@&1', '@&2', '@&3', '@&4'), $Indata);
}
return $Indata;
}
}
__construct 主要的程式就在這
FileCk 檢查檔案是否存在(也包函檢查目錄是否存在)
以下是用來對輸入的東西做替換的function
InDataCk 輸入進來的post和get都一定要經過他,並且返回一組array
CheckInput 替換奇怪的值
這隻程式也會檢查是不是需要載入view,而不需要的就是看url[0]是不是js或是cgi
由於需要control,預先設定的為index和error這兩個control,所以在control資料夾下新增兩個檔案
index.php
error.php
基本上,我們還不需做任何的控制,所以在index.php中只需要產生一個class就可以了(注意,class的名字要和檔名一樣)
index.php
<?php
class index {
function __construct(){
}
}
class index {
function __construct(){
}
}
error.php
<?php
class error{
function __construct(){
}
}
class error{
function __construct(){
}
}
這樣LibBoot.php就會直接找到control底下的這兩個檔案了,不過有了control就需要有view啦~
所以在view這個資料夾底下新增一隻程式和兩個資料夾吧
index
error
View.php
View.php主要是來載入html程式的(之後會講到異步加密,就是從這啦~)
<?php
class View {
function __construct($page, $InData) {
include_once "$page.html";
}
}
如果有標頭和頁腳的話,就是從這加,假設標頭的檔案叫hend.html,頁腳叫foot.htmlclass View {
function __construct($page, $InData) {
include_once "$page.html";
}
}
就加上,就會變成
<?php
class View {
function __construct($page, $InData) {
include_once 'hend.html';
include_once "$page.html";
include_once 'foot.html';
}
}
然後在view資料夾底下新增hend.html和foot.htmlclass View {
function __construct($page, $InData) {
include_once 'hend.html';
include_once "$page.html";
include_once 'foot.html';
}
}
另外,在LibBoot.php裡有提到兩個奇怪的東西:js和cgi
這兩個的作用是,只需要輸出資料和載入js用的
而cgi就直接使用control裡面的程式,所以不用理他了
再來就是需要一個控制js的control這樣才會正常
所以在control資料夾裡新增一個叫js.php的檔案
<?php
class js {
function getJs($obj) {
if($obj){
$arr = array(
'Jquery' => 'lib/js/jquery.min.js'
);
if(in_array($obj, array_keys($arr)))
include $arr[$obj];
else
include $arr['Jquery'];
}else{
echo 'error';
}
}
}
再來就是在lib底下新增一個叫js的資料夾,把你的js檔案丟進去,然後在js.php中新增一個代號和路徑(我設定是在lib底下的js)class js {
function getJs($obj) {
if($obj){
$arr = array(
'Jquery' => 'lib/js/jquery.min.js'
);
if(in_array($obj, array_keys($arr)))
include $arr[$obj];
else
include $arr['Jquery'];
}else{
echo 'error';
}
}
}
這樣要在view裡面拉js的話法就會變成
<script src="<?php echo $_SESSION['SiteUrl'];?>js/Jquery"></script>
而拉資料的話就會變成(用jquery拉)
$.post('<?php echo $_SESSION['SiteUrl'];?>cgi/XXXXX');
這樣做的好處就是從外部完成不知道你的檔案到底是存在哪
然後在 view/index下新增一個叫index.html的檔案吧
裡面就叫你進首頁想給別人看的第一畫面吧(ex: This is index.)
然後在 view/error下新增一個叫404.html的檔案
裡面就是找不到檔給別人看的頁面(ex: Oh, Undefind any Page!!)

沒有留言:
張貼留言