如何用PHP寫一個架構

來源:互聯網
上載者:User
大三學生一枚,第一次寫架構,仿照著THINKPHP 3.2.3,寫的不好請多多包涵

樹形圖

kakoi 檔案夾存放著核心設定檔

Home 檔案夾中包含著控制層Controller

資料層Model

以及模板層View和緩衝Cache

PUBLIC 主要儲存 JS/CSS/等

架構是基於單入口檔案 index.php

此處可以實現分組操作。

 

引入核心配置

buildFile($OTHER);//建立控制器$build->buildController();//建立設定檔$build->buildConfig();$moudle = isset($_GET['moudle'])?$_GET['moudle']:'Home';$controller = isset($_GET['controller'])?$_GET['controller']:'Index';$method = isset($_GET['method'])?$_GET['method']:'Index';// echo $moudle."
";// echo $controller."
";// echo $method."
";//模板存放路徑$path = EVENT."/".$moudle."/View/";$cache = EVENT."/".$moudle."/Cache";$Controller = new Controller();//引入資料庫設定檔$data = require_once(EVENT."/Config/Config.php");require_once("/DB/DB.class.php");$DB = new DB($data);require_once("/View/Template.class.php");//引入模板$arrayConfig = array("suffix" => ".m", //設定模板檔案"templateDir" => $path, //設定模板所在的檔案夾"compileDir" => $cache,"debug" => false,//設定編譯後存放的目錄"cache_htm" => true,//是否需要編譯成靜態html檔案"suffix_cache"=> ".htm",//編譯後的檔案尾碼"cache_time" =>2000,// 多長時間自動更新"php_turn" =>false,//是否支援原生的php代碼"cache_control" => "control.dat",);static $tpl;//擷取模板對象$tpl = Template::getInstance($arrayConfig);//var_dump($tpl); $handle = handle::getInstance();$handle->C($moudle,$controller,$method);//傳入資料庫配置參數

Config 檔案配置資訊

'localhost', 'DB_USER'=>'root',         //使用者名稱'DB_PWD'=>'',              //密碼'DB_NAME'=>'test',         //資料庫名稱'DB_PORT'=>'3306',         //連接埠號碼'DB_TYPE'=>'mysql',   //資料庫類型'DB_CHARSET'=>'utf-8',     //字元集);?>

建立使用者的APP檔案

";static $config="'localhost', 'DB_USER'=>'root',         //使用者名稱'DB_PWD'=>'',              //密碼'DB_NAME'=>'test',         //資料庫名稱'DB_PORT'=>'3306',         //連接埠號碼'DB_TYPE'=>'mysql',   //資料庫類型'DB_CHARSET'=>'utf-8',     //字元集);?>";private static $instance = null;static function getInstance(){if(self::$instance){return self::$instance;}else{self::$instance = new buildDir();return self::$instance;}}public function __construct(){}//建立檔案夾public function buildFile($path = 'Home'){//echo  $path;$this->path = $path;if(!is_dir(EVENT)) mkdir(EVENT,0775,true);if(is_writable(EVENT)){$dirs = array(EVENT."/".$path,EVENT."/Public",EVENT."/Config",EVENT."/".$path."/Controller",EVENT."/".$path."/Model",EVENT."/".$path."/View",EVENT."/".$path."/Cache",);foreach($dirs as $dir){if(!is_dir($dir))mkdir($dir,0775,true);}}}public function buildController(){$controller =EVENT."/".$this->path."/"."Controller";if(is_dir(EVENT."/".$this->path))if(!is_file($controller."/IndexController.class.php")){ $temp=$controller."/IndexController.class.php";$myfile = fopen($temp,"w")or die("Unable to open file!");fwrite($myfile,buildDir::$content);}}public function buildConfig(){$data = EVENT."/Config";if(!is_file($data."/Config.php")){$temp=$data."/Config.php";$myfile = fopen($temp,"w")or die("Unable to open file!");fwrite($myfile,buildDir::$config);}}}

資料庫的CURD操作

table = $Config['DB_NAME']."_";}}public function M($table){$table = strtolower($table);$this->table.= $table;}public function insert($data){$table =$this->table;global $key;global $value;foreach($data as $key_tmp =>$value_tmp){$key.=$key_tmp.',';$value.="'".$value_tmp."'".',';}$key=rtrim($key, ",");$value=rtrim($value, ",");$sql = "insert into ".$table."(".$key.")values(".$value.")";//sql的插入語句  格式:insert into 表(多個欄位)values(多個值)self::$link->query("SET NAMES utf8");  $result=self::$link->query($sql);//調用類自身的//return$result;}public  function update($data){//$table =$this->table; //指定資料的表global $field;foreach($data as $value=>$key){$field.=$value."="."'".$key."'".",";}$field=rtrim($field,",");echo $field."
";$this->update = $field;return $this;//將資料存入到 this 中傳志// $sql="update ".$table." set ".$field." where ".$where."";// echo $sql;// self::$link->query("SET NAMES utf8"); // self::$link->query($sql);}public function where($where){$sql="update ".$this->table." set ".$this->update." where ".$where."";self::$link->query("SET NAMES utf8"); $result = self::$link->query($sql);return $result;}public function select(){self::$link->query("SET NAMES utf8"); $sql = "select * from ".$this->table;$this->sql = $sql;$rs = self::$link->query($sql);$rs->setFetchMode(PDO::FETCH_ASSOC);//返回關聯陣列$result = $rs -> fetchAll();return $result;//return $result;}public function delete($where){$sql = "delete from ".$this->table." where ".$where."";//sql的插入語句 格式:insert into 表(多個欄位)values(多個值)self::$link->query($sql);//調用類自身的}// public function limit($limit){// return "123";// }// public function execute($result = array()){// var_dump($result);// }//魔術方法實現public function __call($fun,$args){if(in_array($fun,array("limit","getField","find"))){echo $fun."
";}}}

View層在之前的部落格中有提到 連結:View層模板

模板主要是通過INDEX.PHP?moudle?=x&controller=x&method=x

之前考慮用偽靜態來實現,後來無論如何接受不到參數就放棄了

代碼如下:

 $nav=$_SERVER["REQUEST_URI"];  // echo "request:".$nav."
"; // $script_name=$_SERVER["SCRIPT_NAME"]; // echo "script_name:".$script_name."
"; // $nav1 = str_replace($script_name,"",$nav); // echo $nav1; // $vars = @explode("/",$nav1); //var_dump($vars); // echo $script_name."
"; // echo substr(ereg_replace("$script_name","",urldecode($nav)),1); // echo $script_name; // $nav1=ereg_replace(".html","",substr(ereg_replace("$script_name","",urldecode($nav)),1)); // echo $nav1; // $vars = @explode("/",$nav1); // $_url = array_chunk($vars,3); // $_GET['m'] = $vars[0]?$vars[0]:'index'; //如果沒有參數,預設訪問index類庫的index方法 // $_GET['a'] = $vars[1]?$vars[1]:'index'; // $_GET['d'] = $vars[2]?$vars[2]:'index'; // unset($_url[0]); // if($_url){ // foreach($_url as $key=>$val){ // @$_GET[$val[0]] = $val[1]; // } // }// var_dump($_GET);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.