Home > Article > Backend Development > Some development experience on MVC in PHP_PHP Tutorial
After nearly a month of research on MVC, I have my own MVC process and framework through the guidance of friends online. However, I feel that there are still many limitations and a lack of flexibility, but I don’t know how to make specific improvements, so today I will publish my process and thoughts, hoping that someone with expertise can give me some advice.
1. Entrance
The entry file can be a single file or multiple files. The one I use now is basically multiple files, but the contents of the entry files are basically the same. This will serve as a basis for future modifications to other entry methods,
<?php require 'command/config.php'; require 'command/app.php'; app::run($config); ?>
Needless to say, everyone can see it first, load the system configuration file, and then load the system configuration through the engine.
2. Engine
public function run($config){ header("Content-type:text/html;charset=utf-8"); self::$config = $config; //加载系统配置 self::copyright(); self::testsystem(); //系统环境 self::setsystem(); //设置系统参数 self::incinfo(); if(!IN_WEB){exit('网站正关闭维护中,请稍候访问!');} defined('KEHENG_DEBUG') or define('KEHENG_DEBUG',true); // 是否调试模式 self::setpath(); //设置系统路径 self::getdatabase(); //测试数据库 self::loadlib(); //加载库 self::getRouteConfig(); //运行路由并加载控制器 }
In the engine, first set the configuration file, then test the system parameters, load the system module, obtain the configured website information file, set the path required by the website, test the database parameters in the system configuration, load the library file, and finally load the route acquisition 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 this 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.
public function getRouteConfig(){ $route_type=self::$config[route][url_type]; switch($route_type){ case 1: //echo $_SERVER['SCRIPT_NAME'].'df250b2156c434f3390392d09b1c9563'; $query_string=$_SERVER['QUERY_STRING']; //echo $_SERVER['REQUEST_URI'].'df250b2156c434f3390392d09b1c9563'; $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('控制器文件不存在'); } }
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,
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('视图文件不存在!'.$temp); } }else{ exit('此页未设置显示模板!'.$temp); } unset($views); }else{ exit('模型文件不存在:'.$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 reading data? When , 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.