Share your development experience with MVC in PHP

  • 2020-05-17 04:50:57
  • OfStack

1. The entrance
The entry file can be a single file or a multi-file. The basic file I use now is multi-file, but the content of the entry file is basically the same, which is the basis for the modification of other entry methods in the future.
 
<?php 
require 'command/config.php'; 
require 'command/app.php'; 
app::run($config); 
?> 

Needless to say, load the system configuration file, and then load the system configuration via the engine.
2. The engine
 
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 being closed for maintenance, please visit later! ');} 
defined('KEHENG_DEBUG') or define('KEHENG_DEBUG',true); //  Debug mode or not  
self::setpath(); // Set the system path  
self::getdatabase(); // Test database  
self::loadlib(); // Load the library  
self::getRouteConfig(); // Run the route and load the controller  
} 

The engine first sets the configuration file, then tests the system parameters, loads the system module, obtains the website information file configured in the configuration, sets the path required by the website, tests the database parameters in the system configuration, loads the library file, and finally loads the route to get the request address. I don't know if this process is right, but I just wrote a set according to my own learning, but there is a lack of cache, the specific cache should be set.
The database test here is to load the wrapper file for the operation on a type 1 database based on which type 1 database is configured.
3. The routing
Following is the last function above, load the controller file and get the requested mode according to the configuration file.
 
public function getRouteConfig(){ 
$route_type=self::$config[route][url_type]; 
switch($route_type){ 
case 1: 
//echo $_SERVER['SCRIPT_NAME'].'<br />'; 
$query_string=$_SERVER['QUERY_STRING']; 
//echo $_SERVER['REQUEST_URI'].'<br />'; 
$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(' The controller file does not exist '); 
} 
} 

4. The controller
The controller file is also pretty simple, just load the model file and view file based on the routing address,
 
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(' No display template is set on this page! '.$temp); 
} 
unset($views); 
}else{ 
exit(' The model file does not exist :'.$url.'.model.php'); 
} 
} 
} 

But one thing to notice is that all the data to be output in the model file is output by a class like views, all the system parameters in the view file in the package, etc. I don't know, this method is not more than 1, the original is to put all the data to be output for encapsulation.
Other template files are encapsulated with class, specific should be how to write down all know, these are just my personal opinion, but the cache should be how to write, it's still a fuzzy concept, whether at the time of reading data, the direction should be read from cache, and then judge the existence of the cache and to judge whether to need to build a cache? Specific operation method is still not very clear. I hope you can give me some advice.

Related articles: