Home > Backend Development > PHP Tutorial > Sharing of development experience on MVC in PHP_PHP Tutorial

Sharing of development experience on MVC in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:18:46
Original
782 people have browsed it

1. Entry
The entry file can be a single file or multiple files. The ones I use now are basically multiple files, but the contents of the entry files are basically the same. Other entry methods can be used for future modifications. To do the basics,

Copy the code The code is as follows:

require 'command/config.php' ;
require 'command/app.php';
app::run($config);
?>

First of all, it goes without saying that everyone can see that, loading System configuration file, and then load the system configuration through the engine.
2. Engine
Copy code The code is as follows:

public function run($config ){
header("Content-type:text/html;charset=utf-8");
self::$config = $config; //Load system configuration
self::copyright() ;
self::testsystem(); //System environment
self::setsystem(); //Set system parameters
self::incinfo();
if(!IN_WEB){exit ('The website is closed for maintenance, please visit later!');}
defined('KEHENG_DEBUG') or define('KEHENG_DEBUG',true); // Whether to debug mode
self::setpath() ; //Set system path
self::getdatabase(); //Test database
self::loadlib(); //Load library
self::getRouteConfig(); //Run route and load Controller
}

First set the configuration file in the engine, then test the system parameters, load the system module, obtain the configured website information file, set the path required by the website, and test the system configuration Database parameters, loading library files, and finally loading the route to obtain the request address. I don’t know if this process is correct, it’s just a set I wrote based on my own learning, but it lacks cache. What should be the specific cache settings?
The database test here is based on which type of database is configured, and then the encapsulation file for the operation of that type of database is loaded.
3. Routing
The following is the last function above, which loads the controller file and obtains the request method according to the configuration file.
Copy code The code is as follows:

public function getRouteConfig(){
$route_type=self::$config[ route][url_type];
switch($route_type){
case 1:
//echo $_SERVER['SCRIPT_NAME'].'
';
$query_string= $_SERVER['QUERY_STRING'];
//echo $_SERVER['REQUEST_URI'].'
';
$urlstr=$_GET['controller'];
break;
case 4:
$url = end(explode('/', $_SERVER["PHP_SELF"]));
$urlstr = strtolower(substr($url,0,-4));
break;
}
if(file_exists(Contr_DIR.'Controller.php')){
require Contr_DIR.'Controller.php';
//echo $urlstr;
$template = self::$config['Templates'];
controller::load($urlstr,$template);
}else{
exit('Controller file does not exist');
}
}

4. Controller
The controller file is also quite simple. It just loads the model file and view file based on the address analyzed by the route. ,
Copy code The code is as follows:

class controller{
public $obj;
public function load( $url,$template){
$config=$template;
if(file_exists(Model_DIR.$url.'.model.php')){
$views = new views;
/ /echo Model_DIR.$url.'.model.php';
require Model_DIR.$url.'.model.php';
$temp = $config[$url][0];
if ($temp!='' && $temp!=null && isset($temp)){
if(file_exists(Templ_DIR.$temp)){
//echo Templ_DIR.$temp;
require Templ_DIR.$temp;
}else{
exit('View file does not exist! '.$temp);
}
}else{
exit('This page has no display template set!'.$temp);
}
unset($views);
}else{
exit('Model file does not exist:'.$url.'.model.php');
}
}
}

But one thing to note is that all the data that needs to be output in the model file is output through a class such as views, including all system parameters in the view file in the package. I don’t know if this method is unnecessary. It turns out that the purpose is to encapsulate all the data to be output.
Other template files are also encapsulated with classes. Experts should know how to write them specifically. These are just my personal opinions, but how to write cache is still a vague concept. Is it When reading data, the direction should be to read the cache, then determine whether the cache exists, and then determine whether the cache needs to be established? The specific operation method is still not very clear. I hope someone can give me some advice.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325452.htmlTechArticle1. Entry The entry file can be a single file or multiple files. The one I use now is basically multiple files. However, the contents of the entry files are basically the same. Other entry methods are used for future modifications...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template