My opinion on MVC in PHP _ PHP Tutorial

Source: Internet
Author: User
Personal opinions on MVC in PHP. 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, 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,
1 2 require 'command/config. php ';
3 require 'command/app. php ';
4 app: run ($ config );
5?>
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

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 being closed for maintenance. please visit it later! ');}
9 defined ('keheng _ debug') or define ('keheng _ debug', true); // whether to DEBUG the mode
10
11 self: setpath (); // set the system path
12 self: getdatabase (); // test the database
13 self: loadlib (); // load the library
14 self: getRouteConfig (); // run the route and load the controller
15}

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.

1 public function getRouteConfig (){
2 $ route_type = self: $ config [route] [url_type];
3 switch ($ route_type ){
4 case 1:
5 // echo $ _ SERVER ['script _ name'].'
';
6 $ query_string = $ _ SERVER ['query _ string'];
7 // echo $ _ SERVER ['request _ URI '].'
';
8 $ urlstr = $ _ GET ['controller'];
9 break;
10 case 4:
11 $ url = end (explode ('/', $ _ SERVER ["PHP_SELF"]);
12 $ urlstr = strtolower (substr ($ url, 0,-4 ));
13 break;
14
15}
16 if (file_exists (Contr_DIR. 'Controller. php ')){
17 require Contr_DIR. 'Controller. php ';
18 // echo $ urlstr;
19 $ template = self: $ config ['templates'];
20 controller: load ($ urlstr, $ template );
21} else {
22 exit ('Controller file does not exist ');
23}
24
25}

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,

1 class controller {
2 public $ obj;
3 public function load ($ url, $ template ){
4
5 $ config = $ template;
6 if (file_exists (Model_DIR. $ url. '. model. php ')){
7 $ views = new views;
8 // echo Model_DIR. $ url. '. model. php ';
9 require Model_DIR. $ url. '. model. php ';
10 $ temp = $ config [$ url] [0];
11 if ($ temp! = ''& $ Temp! = Null & isset ($ temp )){
12 if (file_exists (Templ_DIR. $ temp )){
13 // echo Templ_DIR. $ temp;
14 require Templ_DIR. $ temp;
15} else {
16 exit ('View file does not exist! '. $ Temp );
17}
18} else {
19 exit ('The display template is not set for this page! '. $ Temp );
20}
21 unset ($ views );
22} else {
23 exit ('Model file does not exist: '. $ url.'. model. php ');
24}
25}
26}

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.


From Jiuhu technology

Success ,...

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.