How to use PHP to write a framework for junior students? the first time I wrote the framework, I followed THINKPHP 3.2.3. please forgive me for not writing well.
Tree chart
The kakoi folder stores core configuration files.
The Home folder contains the control layer Controller.
Data Layer Model
And template layer View and Cache
PUBLIC mainly stores JS/CSS/and so on
The framework is based on the single-entry file index. php
Group operations can be performed here.
Introduce core configurations
BuildFile ($ OTHER); // Create a controller $ build-> buildController (); // Create a configuration file $ build-> buildConfig (); $ moudle = isset ($ _ GET ['moudle'])? $ _ GET ['udle']: 'home'; $ controller = isset ($ _ GET ['controller'])? $ _ GET ['controller']: 'index'; $ method = isset ($ _ GET ['method'])? $ _ GET ['method']: 'index'; // echo $ moudle ."
"; // Echo $ controller ."
"; // Echo $ method ."
"; // Template storage path $ path = EVENT. "/". $ moudle. "/View/"; $ cache = EVENT. "/". $ moudle. "/Cache"; $ Controller = new Controller (); // introduce the database configuration file $ data = require_once (EVENT. "/Config. php "); require_once ("/DB. class. php "); $ DB = new DB ($ data); require_once ("/View/Template. class. php "); // introduce the template $ arrayConfig = array (" suffix "=> ". m ", // set the template file" templateDir "=> $ path, // Set the folder where the template is located" compileDir "=> $ cache," debug "=> false, // Set the directory "cache_htm" => true, // whether to compile it into a static html file "suffix_cache" => ". htm ", // The compiled file suffix" cache_time "=> 2000, // How long will it take to automatically update" php_turn "=> false, // whether the native php code "cache_control" => "control. dat ",); static $ tpl; // get the Template object $ tpl = Template: getInstance ($ arrayConfig); // var_dump ($ tpl); $ handle = handle :: getInstance (); $ handle-> C ($ moudle, $ controller, $ method); // input database configuration parameters
Config file configuration information
'Localhost', 'DB _ user' => 'root', // username 'DB _ pwd' => '', // password 'DB _ name' => 'test', // database NAME 'DB _ port' => '123 ', // port number 'DB _ type' => 'mysql', // Database TYPE 'DB _ charset' => 'utf-8', // character set);?>
Create a user's APP file
"; Static $ config ="
'Localhost', 'DB _ user' => 'root', // username 'DB _ pwd' => '', // password 'DB _ name' => 'test', // database NAME 'DB _ port' => '123 ', // port number 'DB _ type' => 'mysql', // Database TYPE 'DB _ charset' => 'utf-8', // character set);?> "; Private static $ instance = null; static function getInstance () {if (self ::$ instance) {return self ::$ instance;} else {self :: $ instance = new buildDir (); return self: $ instance ;}} public function _ construct () {} // create a folder 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 );}}}
Database CURD operations
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 ". $ table. "(". $ key. ") values (". $ value. ")"; // SQL insert statement format: insert into table (multiple fields) values (multiple values) self :: $ link-> query ("set names utf8"); $ result = self: $ link-> query ($ SQL ); // call the class's own // return $ result;} public function update ($ data) {// $ table = $ this-> table; // specify the global $ field; foreach ($ data as $ value => $ key) {$ field. = $ value. "= ". "'". $ key. "'". "," ;}$ field = rtrim ($ field, ","); echo $ field."
"; $ This-> update = $ field; return $ this; // save the data to this to upload logs // $ 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); // returns the associated array $ result = $ rs-> fetchAll (); return $ result; // return $ result;} public function delete ($ where) {$ SQL = "delete from ". $ this-> table. "where ". $ where. ""; // SQL insert statement format: insert into table (multiple fields) values (multiple values) self: $ link-> query ($ SQL ); // call the class's own} // public function limit ($ limit) {// return "123"; //} // public function execute ($ result = array ()) {// var_dump ($ result); // implement public function _ call ($ fun, $ args) {if (in_array ($ fun, array ("limit", "getField", "find") {echo $ fun."
";}}}
The View layer mentioned the link in the previous blog: View layer template
The template mainly uses INDEX. PHP? Moudle? = X & controller = x & method = x
We used pseudo-static to implement it before, and then we gave up whatever we failed to accept the parameter.
The code is as follows:
$ 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'; // If no parameter exists, the index method of the index class library is accessed by default. // $ _ 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 );