Share MVC development experience in PHP

Source: Internet
Author: User
After nearly a month of research on MVC, I have also obtained guidance from some friends on the Internet and developed My Own MVC process and framework. However, I feel that there are still a lot of limitations and I still lack flexibility, but I don't know how to improve it. Today I will release my processes and ideas. I hope someone can give some advice.

After nearly a month of research on MVC, I have also obtained guidance from some friends on the Internet and developed My Own MVC process and framework. However, I feel that there are still a lot of limitations and I still lack flexibility, but I don't know how to improve it. Today I will release my processes and ideas. I hope someone can give some advice.

I. Portal
The entry file can be a single file or multiple files. Currently, I use multiple files, but the content of the entry file is basically the same. This serves as the basis for modifying other entry methods in the future,
The Code is as follows:
Require 'COMMAND/config. php ';
Require 'COMMAND/app. php ';
App: run ($ config );
?>

First of all, you can see that the system configuration file is loaded and then the system configuration is loaded through the engine.
Ii. Engine
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 being closed for maintenance. Please visit it later! ');}
Defined ('keheng _ debug') or define ('keheng _ debug', true); // whether to DEBUG the Mode
Self: setpath (); // set the system path
Self: getdatabase (); // test the database
Self: loadlib (); // load the library
Self: getRouteConfig (); // run the route and load the Controller
}

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, and test the database parameters in the system configuration, load the library file, and finally load the route to get the request address. I don't know if this process is correct. I just wrote a set based on my own learning, but there is no cache in it. What should I do with the specific cache.
Here, the database test is based on the type of database used for configuration, and then loads the encapsulation file for this type of database operation.
Iii. Routing
The following is the last function above. Load the Controller file and obtain the Request Method Based on the configuration file.
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 ');
}
}

Iv. Controller
The Controller file is quite simple, but the model file and View File are loaded Based on the address analyzed by the route,
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 ('the display template is not set for this page! '. $ Temp );
}
Unset ($ views );
} Else {
Exit ('model file does not exist: '. $ url.'. model. php ');
}
}
}

Note that all the data to be output in the model file is output through a class such as views, and all the system parameters in the View File in the package. I don't know if this method is to show more. I originally wanted to encapsulate all the data to be output.
Other template files are encapsulated with classes. You should know how to write them. These are my personal opinions, but what should I write in the cache, it is still a fuzzy concept. Should I read the cache when reading data, then judge whether the cache exists, and then determine whether a cache needs to be established? The specific operation method is not quite clear. Hope you can give some advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.