Learn about PHP mvc development through examples

WBOY
Release: 2016-07-25 09:05:25
Original
1109 people have browsed it
  1. require 'command/config.php';
  2. require 'command/app.php';
  3. app::run($config);
  4. ?>
Copy code

Load the system configuration file, and then load the system configuration through the engine.

2. Engine

  1. public function run($config){
  2. header("Content-type:text/html;charset=utf-8");
  3. self::$config = $config ; //Load system configuration
  4. self::copyright();
  5. self::testsystem(); //System environment
  6. self::setsystem(); //Set system parameters
  7. self::incinfo();
  8. if( !IN_WEB){exit('The website is closed for maintenance, please visit later!');}
  9. defined('KEHENG_DEBUG') or define('KEHENG_DEBUG',true); // Whether to debug mode
  10. self::setpath( ); //Set the system path
  11. self::getdatabase(); //Test the database
  12. self::loadlib(); //Load the library
  13. self::getRouteConfig(); //Run the route and load the controller
  14. }
Copy the code

First, set the configuration file, then test the system parameters, load the system module, obtain the website information file configured in, set the path required for the website, test the database parameters in the system configuration, load the library file, and finally It is to load the route to obtain the request address. The above process is just a set I wrote based on my own learning, but it lacks cache. How should the cache be set up specifically? 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.

  1. /**
  2. 路由
  3. @link http://bbs.it-home.org
  4. */
  5. public function getRouteConfig(){
  6. $route_type=self::$config[route][url_type];
  7. switch($route_type){
  8. case 1:
  9. //echo $_SERVER['SCRIPT_NAME'].'
    ';
  10. $query_string=$_SERVER['QUERY_STRING'];
  11. //echo $_SERVER['REQUEST_URI'].'< ;br />';
  12. $urlstr=$_GET['controller'];
  13. break;
  14. case 4:
  15. $url = end(explode('/', $_SERVER["PHP_SELF"]));
  16. $ urlstr = strtolower(substr($url,0,-4));
  17. break;
  18. }
  19. if(file_exists(Contr_DIR.'Controller.php')){
  20. require Contr_DIR.'Controller.php';
  21. //echo $urlstr;
  22. $template = self::$config['Templates'];
  23. controller::load($urlstr,$template);
  24. }else{
  25. exit('Controller file does not exist');
  26. }
  27. }
Copy code

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.

  1. /**
  2. Controller
  3. @link http://bbs.it-home.org
  4. */
  5. class controller{
  6. public $obj;
  7. public function load($url,$template){
  8. $config=$template;
  9. if( file_exists(Model_DIR.$url.'.model.php')){
  10. $views = new views;
  11. //echo Model_DIR.$url.'.model.php';
  12. require Model_DIR.$url.'.model. php';
  13. $temp = $config[$url][0];
  14. if($temp!='' && $temp!=null && isset($temp)){
  15. if(file_exists(Templ_DIR.$temp) ){
  16. //echo Templ_DIR.$temp;
  17. require Templ_DIR.$temp;
  18. }else{
  19. exit('View file does not exist!'.$temp);
  20. }
  21. }else{
  22. exit('This page does not exist Set display template! '.$temp);
  23. }
  24. unset($views);
  25. }else{
  26. exit('Model file does not exist:'.$url.'.model.php');
  27. }
  28. }
  29. }
Copy code

Note: 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 I want to encapsulate all the data to be output. Other template files are also encapsulated with classes. As for how to write the cache in MVC, it is still a vague concept. When reading data, should the direction be to read the cache, then determine whether the cache exists, and then determine whether a cache needs to be established? Looking forward to the guidance of experts, please join the official QQ group of Scripting School: 161228069 to participate in the discussion.



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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!